Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp1128251rbb; Sun, 25 Feb 2024 22:13:11 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVV6U9w+zey/auw9j7t+HRM1Yf9THlkAhUsjQP7RujD9+l7uSezYcSwOGXIvyAjKvrY8Ray3+ruPPJ3K6GTmZweLJySoHYlfpklGguIxw== X-Google-Smtp-Source: AGHT+IGxrEwDg0M6+9B1sRQw727vcHr8hHrD2TuPkVvOK+V94cFOMvF7YkoMg3aCsUnpTL10RSzh X-Received: by 2002:a17:902:d2c2:b0:1dc:9e62:754c with SMTP id n2-20020a170902d2c200b001dc9e62754cmr3345440plc.4.1708927991048; Sun, 25 Feb 2024 22:13:11 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708927991; cv=pass; d=google.com; s=arc-20160816; b=POBPEOErCkFpDKNyApfimJGJT6XBeGdJNUVPQJv7gyQbd7x/7YVeiQmAnAVqtdAIM+ SRv+5rvhr37euacuXy9ZxRKdQC2qKN+w1LWIH36HaApT6Djd4xYIAX/vkuEQYx7FZyOL Qk/RLi6rKiMPRdV3sLnPWA/Hh7Byka612wT0K0bm5/A3JeaWhDr7pn0FI66Nm/Fw8xnr YRfAspYTs6q7rULI2zGVI77GP/4bQpElYcu+Nu6KfGGlDeD8RgOLOz3B4k7NVTlMrODI M+39dqdwaXY5eAZWjioFRplV961RjS1/BOhpQKGyIjvVYYWewx/vS8FZT0Y6Vh62q8gp 8rTA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=MMeMapqL1uyeZ6GFlk/Lw2IXnw3Pt/OjiS3OMRsVUXk=; fh=jvdeicLAh6GIWgZyF7Y0wLpxAKp5uTVs3fSp19cG3qI=; b=HswNQ9OSmzA7ZGTRfXB1m7RxX35dhCoorT8JmrdeWs0TvqqGoxLOgXMpPRIrEqiLow KwmuO1pNVBUa1EkMBFwMw64p67EiJi35mPw3SgAwGweRuXh7mRE2wSw3oouyJj8Io/aq Jrvxg+3qaFHwhyIK3LYs/lHCaSGMGw4xtmem0h2a1wB6FGvXAfbTNO4CzdUsRDpVpFdQ KEIM/Xq+4qcmsX8/j79+l7k+CFSdw2k7tlLerDvjL7B1qgjMGzCo9xTxu+JopRBM4QqA aGAYxPAmKofQgysSuXeyzcg6pTfZCVso3mt+eSwZ1yjss11kcp+HWxy2FjJJ/zMGqfPa kFxA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iUGSlrfp; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-80581-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80581-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id w7-20020a170902e88700b001dc56a95e98si3217870plg.393.2024.02.25.22.13.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 22:13:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80581-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iUGSlrfp; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-80581-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80581-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 7C3C5281419 for ; Mon, 26 Feb 2024 06:13:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1035D1BC4F; Mon, 26 Feb 2024 06:13:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="iUGSlrfp" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 022871B968; Mon, 26 Feb 2024 06:13:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708927981; cv=none; b=h6QVvlr42c6pslx55DLCfzYTiELSn6RYynizdB4KKIa5oGoLRZlO2Z/MFnQRssnHCPzlOEqgnKuVfIcTGiBlliaWP7HRr/sNbWJgBKhL5V3du2Y4Kr3VRBLJ3i3nq3IIDNvOBDP+nHbbD5OH4mFRaGsy8VRhYNiyna5/VSWfdXg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708927981; c=relaxed/simple; bh=veta97L3ID3YoBQ1toWeRCzS2ZbdS3Bqd91mRo/FXzQ=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=CPqitDYNmFL7pK1NZcciNPQndOzYKK6gXCp4FHdM8wi65i9YeghcShv0pJD2FDMysEdfvaJytVcFRHyIMK8+/XCVoaKNS0bTslrzj9zrPOhxCf3tolLQ70KWk1L5h0vbQAJr3xdQXNk42j97Unfyi5ThiOr0L0jAlUleRHo/6Eo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iUGSlrfp; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8227CC433C7; Mon, 26 Feb 2024 06:13:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708927980; bh=veta97L3ID3YoBQ1toWeRCzS2ZbdS3Bqd91mRo/FXzQ=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=iUGSlrfp+wGy5nEZNmrjYIOfJDv4kW8Sr4uFCIrX1DM6/tYuRcGRLGr0EVpIuZPNO WpWtF/H2SHnqF85ulOQSZjzo1ldZkkPL0YovwP4Zp8cjlh3gvo4yW4+mRgnd7jaNGq VTzQGId1urXXOR7OBRWq78jX9CTWKvSIQt76Q9Sj8lAP6ejkHL3s+byO17Cg/kddFl URCkgmgKRpubWI/neyhC2zfn48hiS0o3Y63m6xAY7jZ08Q4pC0/XEBr2osu9Y0wKDZ qxijQJY82W4rgljpsDG1+O/sDwwS2j2/QyaTA32qAEjl3ZDA8OdN7ttKwyyC6pMuHl vBfiGRAbY4S+A== Received: by mail-ej1-f44.google.com with SMTP id a640c23a62f3a-a3ddc13bbb3so389018466b.0; Sun, 25 Feb 2024 22:13:00 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCUna2ZnwvIZ5ZS5cqoYuDjYRSBvWB08Cfq57vbBGVJOg0P9La5zdQatg7C0CepuohFLWX2kUR+VORBfsPD8BcWHoQFusUwPx5LjUilhqYaHDMe2qKr9YBmOAujP0I7IuJTG X-Gm-Message-State: AOJu0Yyv1JayS9khC1QcHUOxD/nzA0stQyR0NAWxMiEjj7bjYWgfN5Zc JGrp5XOykfYe3ENELcyaHv27x7luLQS3nRnaYBWTJGMEWr7yg3j32OozSqKy6WSFFkDTq19UCtD 8KS3/v6DehkSd7mjnrWnxxz83vMM= X-Received: by 2002:a17:906:d923:b0:a3f:1139:5a6b with SMTP id rn3-20020a170906d92300b00a3f11395a6bmr4636609ejb.32.1708927978885; Sun, 25 Feb 2024 22:12:58 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240222032803.2177856-1-maobibo@loongson.cn> <20240222032803.2177856-4-maobibo@loongson.cn> In-Reply-To: From: Huacai Chen Date: Mon, 26 Feb 2024 14:12:49 +0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v5 3/6] LoongArch: KVM: Add cpucfg area for kvm hypervisor To: maobibo , Jiaxun Yang Cc: Tianrui Zhao , Juergen Gross , Paolo Bonzini , loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, kvm@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Feb 26, 2024 at 10:04=E2=80=AFAM maobibo wrot= e: > > > > On 2024/2/24 =E4=B8=8B=E5=8D=885:13, Huacai Chen wrote: > > Hi, Bibo, > > > > On Thu, Feb 22, 2024 at 11:28=E2=80=AFAM Bibo Mao = wrote: > >> > >> Instruction cpucfg can be used to get processor features. And there > >> is trap exception when it is executed in VM mode, and also it is > >> to provide cpu features to VM. On real hardware cpucfg area 0 - 20 > >> is used. Here one specified area 0x40000000 -- 0x400000ff is used > >> for KVM hypervisor to privide PV features, and the area can be extende= d > >> for other hypervisors in future. This area will never be used for > >> real HW, it is only used by software. > > After reading and thinking, I find that the hypercall method which is > > used in our productive kernel is better than this cpucfg method. > > Because hypercall is more simple and straightforward, plus we don't > > worry about conflicting with the real hardware. > No, I do not think so. cpucfg is simper than hypercall, hypercall can > be in effect when system runs in guest mode. In some scenario like TCG > mode, hypercall is illegal intruction, however cpucfg can work. Nearly all architectures use hypercall except x86 for its historical reasons. If we use CPUCFG, then the hypervisor information is unnecessarily leaked to userspace, and this may be a security issue. Meanwhile, I don't think TCG mode needs PV features. I consulted with Jiaxun before, and maybe he can give some more comments. > > Extioi virtualization extension will be added later, cpucfg can be used > to get extioi features. It is unlikely that extioi driver depends on > PARA_VIRT macro if hypercall is used to get features. CPUCFG is per-core information, if we really need something about extioi, it should be in iocsr (LOONGARCH_IOCSR_FEATURES). Huacai > > Regards > Bibo Mao > > > > > Huacai > > > >> > >> Signed-off-by: Bibo Mao > >> --- > >> arch/loongarch/include/asm/inst.h | 1 + > >> arch/loongarch/include/asm/loongarch.h | 10 ++++++ > >> arch/loongarch/kvm/exit.c | 46 +++++++++++++++++-------= -- > >> 3 files changed, 41 insertions(+), 16 deletions(-) > >> > >> diff --git a/arch/loongarch/include/asm/inst.h b/arch/loongarch/includ= e/asm/inst.h > >> index d8f637f9e400..ad120f924905 100644 > >> --- a/arch/loongarch/include/asm/inst.h > >> +++ b/arch/loongarch/include/asm/inst.h > >> @@ -67,6 +67,7 @@ enum reg2_op { > >> revhd_op =3D 0x11, > >> extwh_op =3D 0x16, > >> extwb_op =3D 0x17, > >> + cpucfg_op =3D 0x1b, > >> iocsrrdb_op =3D 0x19200, > >> iocsrrdh_op =3D 0x19201, > >> iocsrrdw_op =3D 0x19202, > >> diff --git a/arch/loongarch/include/asm/loongarch.h b/arch/loongarch/i= nclude/asm/loongarch.h > >> index 46366e783c84..a1d22e8b6f94 100644 > >> --- a/arch/loongarch/include/asm/loongarch.h > >> +++ b/arch/loongarch/include/asm/loongarch.h > >> @@ -158,6 +158,16 @@ > >> #define CPUCFG48_VFPU_CG BIT(2) > >> #define CPUCFG48_RAM_CG BIT(3) > >> > >> +/* > >> + * cpucfg index area: 0x40000000 -- 0x400000ff > >> + * SW emulation for KVM hypervirsor > >> + */ > >> +#define CPUCFG_KVM_BASE 0x40000000UL > >> +#define CPUCFG_KVM_SIZE 0x100 > >> +#define CPUCFG_KVM_SIG CPUCFG_KVM_BASE > >> +#define KVM_SIGNATURE "KVM\0" > >> +#define CPUCFG_KVM_FEATURE (CPUCFG_KVM_BASE + 4) > >> + > >> #ifndef __ASSEMBLY__ > >> > >> /* CSR */ > >> diff --git a/arch/loongarch/kvm/exit.c b/arch/loongarch/kvm/exit.c > >> index 923bbca9bd22..6a38fd59d86d 100644 > >> --- a/arch/loongarch/kvm/exit.c > >> +++ b/arch/loongarch/kvm/exit.c > >> @@ -206,10 +206,37 @@ int kvm_emu_idle(struct kvm_vcpu *vcpu) > >> return EMULATE_DONE; > >> } > >> > >> -static int kvm_trap_handle_gspr(struct kvm_vcpu *vcpu) > >> +static int kvm_emu_cpucfg(struct kvm_vcpu *vcpu, larch_inst inst) > >> { > >> int rd, rj; > >> unsigned int index; > >> + > >> + rd =3D inst.reg2_format.rd; > >> + rj =3D inst.reg2_format.rj; > >> + ++vcpu->stat.cpucfg_exits; > >> + index =3D vcpu->arch.gprs[rj]; > >> + > >> + /* > >> + * By LoongArch Reference Manual 2.2.10.5 > >> + * Return value is 0 for undefined cpucfg index > >> + */ > >> + switch (index) { > >> + case 0 ... (KVM_MAX_CPUCFG_REGS - 1): > >> + vcpu->arch.gprs[rd] =3D vcpu->arch.cpucfg[index]; > >> + break; > >> + case CPUCFG_KVM_SIG: > >> + vcpu->arch.gprs[rd] =3D *(unsigned int *)KVM_SIGNATURE= ; > >> + break; > >> + default: > >> + vcpu->arch.gprs[rd] =3D 0; > >> + break; > >> + } > >> + > >> + return EMULATE_DONE; > >> +} > >> + > >> +static int kvm_trap_handle_gspr(struct kvm_vcpu *vcpu) > >> +{ > >> unsigned long curr_pc; > >> larch_inst inst; > >> enum emulation_result er =3D EMULATE_DONE; > >> @@ -224,21 +251,8 @@ static int kvm_trap_handle_gspr(struct kvm_vcpu *= vcpu) > >> er =3D EMULATE_FAIL; > >> switch (((inst.word >> 24) & 0xff)) { > >> case 0x0: /* CPUCFG GSPR */ > >> - if (inst.reg2_format.opcode =3D=3D 0x1B) { > >> - rd =3D inst.reg2_format.rd; > >> - rj =3D inst.reg2_format.rj; > >> - ++vcpu->stat.cpucfg_exits; > >> - index =3D vcpu->arch.gprs[rj]; > >> - er =3D EMULATE_DONE; > >> - /* > >> - * By LoongArch Reference Manual 2.2.10.5 > >> - * return value is 0 for undefined cpucfg inde= x > >> - */ > >> - if (index < KVM_MAX_CPUCFG_REGS) > >> - vcpu->arch.gprs[rd] =3D vcpu->arch.cpu= cfg[index]; > >> - else > >> - vcpu->arch.gprs[rd] =3D 0; > >> - } > >> + if (inst.reg2_format.opcode =3D=3D cpucfg_op) > >> + er =3D kvm_emu_cpucfg(vcpu, inst); > >> break; > >> case 0x4: /* CSR{RD,WR,XCHG} GSPR */ > >> er =3D kvm_handle_csr(vcpu, inst); > >> -- > >> 2.39.3 > >> > >