Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp759212lqa; Sun, 28 Apr 2024 03:06:22 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV0hsXNaeZW8alq71ywr40FcPuRgtkP/YplIPI7i4wRGX4SRjwbehWNPFfKKnhh7tAk8RClP7khA8SYOfaW80PuFBJwum5zerkPXzF/jA== X-Google-Smtp-Source: AGHT+IFb3lVG+zT4W1LudlG7dFK0l+J+OMzgZwn38VAW+SPtxZkGngwUiydANoBCdJ8vfBWP8ImY X-Received: by 2002:a05:6300:808b:b0:1a9:84f6:dcb6 with SMTP id ap11-20020a056300808b00b001a984f6dcb6mr7459892pzc.57.1714298782196; Sun, 28 Apr 2024 03:06:22 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714298782; cv=pass; d=google.com; s=arc-20160816; b=njB+z+gVs6+esLs7Fu2IRnkRcfizXoC596kBUGt913YGHzQU+2kaP9+Hmz2bst0TxJ sXw4Arom82PwRDSwT69Mjv/40/sfFJQETKT7vLAVjyhABIT8mIqlvGvDCaulhRWMV50f OCmOIR44z66BD9xB3pKKmbn3GhNiMUM7xA0G5ERSj9ge2xBDDBtwOe7A7Cdza0ojwRZn MJxUiH+lVoDldPICCCgo2cUobpL6Fe6F/sPdFbbYi00r4TO0ZPVsSxbJP6fJPLi75EmN Mao0IhKe2r/Gpzjgulfc91rAyOS+g1JgTeEQ65GY+f4Zz/Hu2Og/xVHISMsqqgYiT5fx ZKBA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=dsfun2TCphj9XSPUgwWP/7gX4zL2KeLktmeeT6vMFBI=; fh=gX94dkNqn304bMpoKWQ3H95iV7TmWzUlV3lg4yusrXk=; b=IK2RGA0JAvRkhQiAM4e9jOkSqb5DL/HSOrUyr8wxiNXLbeW2tfiQHeZmJ6m/M30ucF p03xIqyJLdvYKGbxaFFWQF+65LJKxfyzsB0amAcQGcrNK7jecWuUDnlKnOOhXk4Uhz+Y /Q3tVezUhgfCDIDqOoVP/G8lCl1ERRmG4y9hOcQulKXvNm5aHLdPTAiTYjpcLSi29900 dc37ZMW060ze+15JIS8BcJqnVrG2c10nS6E/AnhSPmvcZnTQmkk8olM15BbLqMsOJRv2 3FwcFU2m4QUU1XTcQboJ8ETDkDRBXd+c7Hw1hrQidAd+ukLQZFeGk7SUM0QWJGzv/Oul W6DA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=loongson.cn); spf=pass (google.com: domain of linux-kernel+bounces-161364-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-161364-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id s21-20020a634515000000b005e42b4c97f2si17883235pga.289.2024.04.28.03.06.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Apr 2024 03:06:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-161364-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=loongson.cn); spf=pass (google.com: domain of linux-kernel+bounces-161364-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-161364-linux.lists.archive=gmail.com@vger.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 CADC7281DFE for ; Sun, 28 Apr 2024 10:06:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 56D625C8F4; Sun, 28 Apr 2024 10:05:29 +0000 (UTC) Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A8DE955E58; Sun, 28 Apr 2024 10:05:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714298728; cv=none; b=Om4D25LVSFebRleX6ap/2viAFWTaYEWxqF1Hhn7lEw/16YidgUM41ZOy3XMy7XDjsSmJ3zpGWv7ZWIb2ydsfFpEv7KbK3sl1EaNtcyKqWu54se/uXHOMcp8x0xJ3HSDNEfQIM4NqpvGcGqujnj9UsYcaPjGIpiqnw1v7bPHivSk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714298728; c=relaxed/simple; bh=GpJHmOstZ974xdIjcJBta/hwNNTLHPT/Ph5JGovCEBw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=R+YkP0XRlGQ+2Isgh+LcIsd0+yXTIWlL6NDqhjxpx9o2pEgGtSi06hZSInx0ibTWSg6y6QeBk8cqzvzH0djAJLBtHycvD8Fp5PyWWtSqU0LgwE7m+ipMKm6ST5xzjfEXmOU8uMGeN7jqfImnFa41L8aGhDdyXrFpir8PXg+9GKs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [10.2.5.213]) by gateway (Coremail) with SMTP id _____8CxiOlkHy5mul0EAA--.3442S3; Sun, 28 Apr 2024 18:05:24 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by localhost.localdomain (Coremail) with SMTP id AQAAf8DxsFVeHy5maTIIAA--.5646S5; Sun, 28 Apr 2024 18:05:22 +0800 (CST) From: Bibo Mao To: Huacai Chen , Tianrui Zhao , Juergen Gross , Paolo Bonzini , Jonathan Corbet Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, kvm@vger.kernel.org Subject: [PATCH v8 3/6] LoongArch: KVM: Add cpucfg area for kvm hypervisor Date: Sun, 28 Apr 2024 18:05:15 +0800 Message-Id: <20240428100518.1642324-4-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240428100518.1642324-1-maobibo@loongson.cn> References: <20240428100518.1642324-1-maobibo@loongson.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID:AQAAf8DxsFVeHy5maTIIAA--.5646S5 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== 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 extended for other hypervisors in future. This area will never be used for real HW, it is only used by software. Signed-off-by: Bibo Mao --- arch/loongarch/include/asm/inst.h | 1 + arch/loongarch/include/asm/loongarch.h | 10 +++++ arch/loongarch/kvm/exit.c | 53 ++++++++++++++++++-------- 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/arch/loongarch/include/asm/inst.h b/arch/loongarch/include/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 = 0x11, extwh_op = 0x16, extwb_op = 0x17, + cpucfg_op = 0x1b, iocsrrdb_op = 0x19200, iocsrrdh_op = 0x19201, iocsrrdw_op = 0x19202, diff --git a/arch/loongarch/include/asm/loongarch.h b/arch/loongarch/include/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..552a2fedbe44 100644 --- a/arch/loongarch/kvm/exit.c +++ b/arch/loongarch/kvm/exit.c @@ -206,10 +206,44 @@ 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; + unsigned long plv; + + rd = inst.reg2_format.rd; + rj = inst.reg2_format.rj; + ++vcpu->stat.cpucfg_exits; + index = vcpu->arch.gprs[rj]; + + /* + * By LoongArch Reference Manual 2.2.10.5 + * Return value is 0 for undefined cpucfg index + * + * Disable preemption since hw gcsr is accessed + */ + preempt_disable(); + plv = kvm_read_hw_gcsr(LOONGARCH_CSR_CRMD) >> CSR_CRMD_PLV_SHIFT; + switch (index) { + case 0 ... (KVM_MAX_CPUCFG_REGS - 1): + vcpu->arch.gprs[rd] = vcpu->arch.cpucfg[index]; + break; + case CPUCFG_KVM_SIG: + /* Cpucfg emulation between 0x40000000 -- 0x400000ff */ + vcpu->arch.gprs[rd] = *(unsigned int *)KVM_SIGNATURE; + break; + default: + vcpu->arch.gprs[rd] = 0; + break; + } + + preempt_enable(); + return EMULATE_DONE; +} + +static int kvm_trap_handle_gspr(struct kvm_vcpu *vcpu) +{ unsigned long curr_pc; larch_inst inst; enum emulation_result er = EMULATE_DONE; @@ -224,21 +258,8 @@ static int kvm_trap_handle_gspr(struct kvm_vcpu *vcpu) er = EMULATE_FAIL; switch (((inst.word >> 24) & 0xff)) { case 0x0: /* CPUCFG GSPR */ - if (inst.reg2_format.opcode == 0x1B) { - rd = inst.reg2_format.rd; - rj = inst.reg2_format.rj; - ++vcpu->stat.cpucfg_exits; - index = vcpu->arch.gprs[rj]; - er = EMULATE_DONE; - /* - * By LoongArch Reference Manual 2.2.10.5 - * return value is 0 for undefined cpucfg index - */ - if (index < KVM_MAX_CPUCFG_REGS) - vcpu->arch.gprs[rd] = vcpu->arch.cpucfg[index]; - else - vcpu->arch.gprs[rd] = 0; - } + if (inst.reg2_format.opcode == cpucfg_op) + er = kvm_emu_cpucfg(vcpu, inst); break; case 0x4: /* CSR{RD,WR,XCHG} GSPR */ er = kvm_handle_csr(vcpu, inst); -- 2.39.3