Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp6186292iob; Tue, 10 May 2022 12:17:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyYxunK0cPvdQBT5cGSjqfDYiXsCOcTV6DUfZ8yxZgYygX7mjClXl37s2poei+oG0OTz960 X-Received: by 2002:a17:902:e809:b0:15e:c67d:14c5 with SMTP id u9-20020a170902e80900b0015ec67d14c5mr21368389plg.13.1652210273124; Tue, 10 May 2022 12:17:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652210273; cv=none; d=google.com; s=arc-20160816; b=zKcDwQfC6sxA8Vgdn/kIfrlMIyN0jIUUkXnZFylrBG7/0BRGGT/u/AS34RpBgarPS1 SDI2qHfTsq7RiU+Q3bL7cQNsC1OkZZMtwOUeDMCblrO9mmHV5Iah6yPap7VBH9COtb4q GtqED0B4GWQcGNyXKkxKfGYecvEebRbKHUd8vyiSIowk0qyUeCDPOWI3iE8m3STQ4Wzf RDy0I0qz33Qu4ua+ipQCNyI4CE7BkFH+3d68G3QKoMHw1oXOs3h1Ds4pcwsFuzuUSKQO ulWxA1N+vrqIcrkmPy2pR0zu9SFbGP1oGJbuBZzFkRVHvKx34hkZWAmDUPAD7EyGR+hz 6fCw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=Zy0q38hbFw40Sq+7rWYl2W9NS/43Or3wh3FVtf7AHik=; b=O2RpVT3nN/Y8RaR7V147SUoe7ZHepIxvl85eufy2I8vEE8NV+DZ2HWgVmYmqUr+y9m 63o5FW13JzOIyzPkveMKk4RGol4xfAFsDY0VxwpJABOZMF2BGZX64FreqZQq6Z0Y9O+H v+Vv9qPZmdWrfDtdTCFLfNm/y/PXwifbm2XlV+6VP8nFpnnqULmNYXVHvGbF5qRdno+j 3mFcluhjsV98rTche0NlbSxkGwMC4KiETzXmPMuxHdp44+4tnOKyBpSI94Du0xyC42UY Sl9MDxgHcW7GJYF++zZ6vgIneSIHgF52CQmAxz3BAeS+rO7v7Sr8uFaHLuzlpE2Y61pY tclg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@brainfault-org.20210112.gappssmtp.com header.s=20210112 header.b=S7+1zDRF; 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 w16-20020a1709027b9000b0015ea33a78cbsi3825103pll.80.2022.05.10.12.17.36; Tue, 10 May 2022 12:17:53 -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=@brainfault-org.20210112.gappssmtp.com header.s=20210112 header.b=S7+1zDRF; 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 S1345638AbiEJPWz (ORCPT + 99 others); Tue, 10 May 2022 11:22:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345427AbiEJPTz (ORCPT ); Tue, 10 May 2022 11:19:55 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E66AF1B7B5 for ; Tue, 10 May 2022 07:59:44 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id b19so24196489wrh.11 for ; Tue, 10 May 2022 07:59:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brainfault-org.20210112.gappssmtp.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Zy0q38hbFw40Sq+7rWYl2W9NS/43Or3wh3FVtf7AHik=; b=S7+1zDRFW9/cYqjSR86okpMRKuCC9XcU9HyEOVI2BNsXl7oefcfTrVKEbAWFqr+Mxf uptJofTLpL7TCXt2VTkiLvhC59q8UuYlZ/SQmybkiebb25kRPy3K42CfmG6Dn2xP/XVp zsIHlsh2XX4D8GAuzGm5ewiMIjo/jbwBQZee+jMVNFMguCTD5YB4LjOmvfkFQCPueuur v7QbxpiaShFDgfuJ04VrJLN+X3oIb8FWX3x6vp8YsA+W0IyrLkQ53iEVUx3zdArjlrCN ym004kK0GwDJ5ijF4+rbYGtoQoCAq9b49sXBvyAk6/tVVqa9yglkYfhec6+XixvAM4dp bXsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Zy0q38hbFw40Sq+7rWYl2W9NS/43Or3wh3FVtf7AHik=; b=fvQoDX3EqH8YpoyvhlxWmNZpfX9VNWHUbDglSCtTyNm3imWsh2NmGE8rhY79ipMYTY UvPYwN/Wr0Ce/fkQBcJViU1GCjQaCStGX3ZUJCRQ5JnwS91f4RWIBy6klInUR/VrZgqq ae/4Qc/GbNBAgj8NFchaAZxJB0LTaacQTc34wKIXz/4488rCv4ZnkSSL+Y5WaokPqzmi 5VDPmVVJajDt/7cp+eHti0PnmmsFSOtBdsL17tuMhMf1zY/9MLJraNbOgAMW9mb89+LB qCD1OGxg1PKe73O+DgzgWxoOuH3UrPlEYuhU5wpmY2mH2LObaLuwa9pvWzeE7Yjfn6Ms SGCg== X-Gm-Message-State: AOAM533Q334mNyzBI/+Wl/wQPTYk6qs/yGWVx0oM6uNhghsI9YJ/Ov1A nQinJnYr/zRYX7osQaVXxGNVx7kn3Ds25fW7G9W3YWyGFvUtEQ== X-Received: by 2002:a5d:6c6b:0:b0:1ea:77ea:dde8 with SMTP id r11-20020a5d6c6b000000b001ea77eadde8mr19048585wrz.690.1652194782989; Tue, 10 May 2022 07:59:42 -0700 (PDT) MIME-Version: 1.0 References: <20220509182937.1881849-1-atishp@rivosinc.com> In-Reply-To: <20220509182937.1881849-1-atishp@rivosinc.com> From: Anup Patel Date: Tue, 10 May 2022 20:29:31 +0530 Message-ID: Subject: Re: [PATCH v3] RISC-V: KVM: Introduce ISA extension register To: Atish Patra Cc: KVM General , Atish Patra , Damien Le Moal , DTML , Jisheng Zhang , Krzysztof Kozlowski , "open list:KERNEL VIRTUAL MACHINE FOR RISC-V (KVM/riscv)" , "linux-kernel@vger.kernel.org List" , linux-riscv , Palmer Dabbelt , Paul Walmsley , Rob Herring Content-Type: text/plain; charset="UTF-8" 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_NONE, 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 On Mon, May 9, 2022 at 11:59 PM Atish Patra wrote: > > 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 Looks good to me. Queued this patch for 5.19 Thanks, Anup > --- > 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 >