Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp5238284iob; Mon, 9 May 2022 11:39:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw7R4oIzy7XMq6I8KqUvwJ23/CsGNC0k1hLBdODqg6CHTXwHX0F8i9wRk1/vCc1AN3LQWKJ X-Received: by 2002:a05:622a:650:b0:2e1:e5cb:e47d with SMTP id a16-20020a05622a065000b002e1e5cbe47dmr16235591qtb.223.1652121553408; Mon, 09 May 2022 11:39:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652121553; cv=none; d=google.com; s=arc-20160816; b=l+dC2Dcwkeblfvosaa1L3M1k1/wmmFYLXBn5HKaMxzmReRU9h6hGRqHxUdMCttZgV3 6sXtqnkxtMAg1M9E9lpJwj6bDWMfxM4iQxNOLV33aXSx5gfgGbQhpFKet7LxAIsc/QyJ iSvj5klcdbpYz4qUdvhx70Lssjm1OVZlwAa0G+SxpEyaddxBn3SMfrz8qMhGg/CA7rZs odTxAnqywbhsckBMYI6sCVQc1Igm1SA6Dm4zxb02fDsuCv9pDot8WB+xNtLALIG/nUBH DCKXkd+9XHlVL9hT2jetbctSK50hSvI6bXiQzicauDHDPpxDXiWSbAQqe77s+RCvCYdM S/7A== 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=kTCI93KHbih05ABsOG0D6ylKWGIQnpf75AAZhBgyXJ4=; b=L8mihpNQYSjD/xhJEpKICSv2eC1ZEBgPA9fmvc8pYWWpgtnDOfW5Nbr6s244U3zDvO e6KEy96HSH/4dgL+NUJ1q1b2jJO4QKHmBdaVs3x80KcOc4KvLfT1YeO4RNEg26twj9xi nr4ImgTSLbv1iVOua+ICB1xH+ehdtCQ2VjdEzSTvPEgqpLU4sb+CnClN/V90I0V6Uyn6 HTJrCJoqG9YnvJ+oWh6cbcnUv/P4Cpvopxcn9/1VzX4YG5OMndWu+ZHYLNcHJVK0wWia 6lRxU1S13UlhMLy65LmT4l0Ufkwas86RmMUM0RFeIfQrOf3tkTREpIPXBERamQ46Usam p91g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b="Yb3Aw7/3"; 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 kl29-20020a056214519d00b0044447274f13si9483668qvb.512.2022.05.09.11.39.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 May 2022 11:39:13 -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="Yb3Aw7/3"; 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 0DBCF31DFF; Mon, 9 May 2022 11:31:03 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240199AbiEISet (ORCPT + 99 others); Mon, 9 May 2022 14:34:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240197AbiEISej (ORCPT ); Mon, 9 May 2022 14:34:39 -0400 Received: from mail-lf1-x134.google.com (mail-lf1-x134.google.com [IPv6:2a00:1450:4864:20::134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0CBD917ABE for ; Mon, 9 May 2022 11:30:43 -0700 (PDT) Received: by mail-lf1-x134.google.com with SMTP id bq30so25318533lfb.3 for ; Mon, 09 May 2022 11:30:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=kTCI93KHbih05ABsOG0D6ylKWGIQnpf75AAZhBgyXJ4=; b=Yb3Aw7/3HkcHxcJp6s9T2JzxBEPmAAXb7m4ayDSWQS2oi9mJXYaEZpmwb8qtsdyr3I RXxwQElP0TlpznUW01tGNc4Oy6Fu07TN2O+hMDJGP4/cHP9z9nibfWFxP81RxjWamW1D OkFpFkPzNQfMO/lo6E1JPHUJGO7eZ7SPV96W+P++ULU38c5ItSiJhVU9WGm6Vl5bD1jR 4ClQsSj6ytUu1otZFaD6fG8TO1ExpGhpDLZMd+ZFiKoqqqmqFDuylWHH/C4oEus0slQi D1PRPAqYUCbI3r/VNF1pQPjaHxgMcBNdTXGfb7XLE3/de60QrSBfQjzUqx/Q3UXkcewP g0XA== 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=kTCI93KHbih05ABsOG0D6ylKWGIQnpf75AAZhBgyXJ4=; b=f4ThHOqo5Yh2mmJPWFoZR9/k75w7WlrEYyXn1QRrzSJEia22UItU6aRclnAwUXznMv IsT01oW3AKNthy3NDv1xB6mR0zBAe+qnXf5B0cR4YRU09NtocxBtell0woh2u34jj09f /GtxZYlJLIKJ4JVBvy0anRHnsgYiqTQvsoyAM3q5+mezg8KIN87bY+fiIeVWWq4vnLoG fB8PxOYyEDwWL7JOfAHElulNg4fUDAhEGSOv7ZWBJwJ+UTSLl8ubzU4nYM/K1eB2r9fJ d2kDjSd0zDqVz/IcedPUPDPYJ5/b1+R93UqwlZmuZIv+fW4/+zMTLwNZ2NFcNvj/8eVr a54A== X-Gm-Message-State: AOAM531SFQ9EWPwDn6We/xqhHPX2QhRv/G9FhwF9lxq5xaq18vTYryu/ oOpFW/5IXT4+HvhGo1AbECz63/jyX3NiYc9GxEpJYA== X-Received: by 2002:a05:6512:2090:b0:472:2764:1f0c with SMTP id t16-20020a056512209000b0047227641f0cmr14020475lfr.482.1652121041364; Mon, 09 May 2022 11:30:41 -0700 (PDT) MIME-Version: 1.0 References: <20220422150519.3818093-1-atishp@rivosinc.com> In-Reply-To: From: Atish Kumar Patra Date: Mon, 9 May 2022 11:30:30 -0700 Message-ID: Subject: Re: [v2 PATCH] RISC-V: KVM: Introduce ISA extension register To: Anup Patel Cc: KVM General , Atish Patra , DTML , Jisheng Zhang , Krzysztof Kozlowski , "linux-kernel@vger.kernel.org List" , linux-riscv , Palmer Dabbelt , Paul Walmsley , Rob Herring , "open list:KERNEL VIRTUAL MACHINE FOR RISC-V (KVM/riscv)" Content-Type: text/plain; charset="UTF-8" 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 On Sun, May 8, 2022 at 10:25 PM Anup Patel wrote: > > On Fri, Apr 22, 2022 at 8:38 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 > > --- > > Changes from v1->v2: > > 1. Sending the patch separate from sstc series as it is unrelated. > > 2. Removed few redundant lines. > > > > The kvm tool patches can be found here. > > > > https://github.com/atishp04/kvmtool/tree/sstc_v2 > > > > --- > > arch/riscv/include/uapi/asm/kvm.h | 20 +++++++ > > arch/riscv/kvm/vcpu.c | 98 +++++++++++++++++++++++++++++++ > > 2 files changed, 118 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 aad430668bb4..93492eb292fd 100644 > > --- a/arch/riscv/kvm/vcpu.c > > +++ b/arch/riscv/kvm/vcpu.c > > @@ -365,6 +365,100 @@ 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(NULL, host_isa_ext)) > > This should be "__riscv_isa_extension_available(vcpu->arch.isa, host_isa_ext)". Ahh yes. Thanks for catching that. > > > + 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. > > + */ > > Use double-winged comment-block for multi-line comments. Fixed. > > > + 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 +476,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 +499,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.25.1 > > > > Regards, > Anup