Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4284919rdb; Thu, 14 Sep 2023 18:54:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF29P6m/94btDKIYo/oWs9g5wxkQx/RlFxY7zifn6e9b35pVgzrIVxMD6aSk1lHekC59X5R X-Received: by 2002:a17:902:cec6:b0:1bb:d7d4:e2b with SMTP id d6-20020a170902cec600b001bbd7d40e2bmr381560plg.0.1694742879065; Thu, 14 Sep 2023 18:54:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694742879; cv=none; d=google.com; s=arc-20160816; b=i/pCCmQqqLZDnQ2QqTFa8hLRVzppqf6aMFFlnoKylaSz8VfhraXyRDKFjESBRmNFwb 9yO/v4ZgBs7cb2/aejVFl+q97NJdN17OwuSMW4L1z8TEM80LMUBnJcvVk6Ew/xFAXeDX IUDQwhqcuor2cA9fCJ/tZyDzHTVjklJjeDkqd0gAlAWh4Jr1k+T4tAIPj9gxrGzpc1xM 3GZDyt8ZWjsBUqZAHqm+PuFRxot1c9a+1tuJ6eEzubZ+fkbgi6oxgDLzTIVR392ScAkZ dIASNPru3adS2cr+UjGBzLzKhWQPmiwDpnQaD64+XJ+6/sg/KLof+Q8i3niDw41E7iKQ 57yw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=2QZB1FXk3tADawlDP43Kh+fKzZ0gfhPtUADujPoJEvo=; fh=ejoaVAUUbBXsA/6jM/beuI9xCYtV2osdR9SpI7hjY84=; b=wBC+SwPRDbGZF28xSVTLybzWYvdO5fa+VXyIUQ/CFZ+bhNFUkB7+OIyBccjfMR11s+ g9PTWNrUQqy7HDth2KgWb7wwJ0WILwAoaCRQLFRzcKiELiL3FQ+i+06iY8/XC8PEeHtJ cACV3wnuDXik6ny2euxIFKSG9nx6zE+Y1BrDSrPcDPFEWGMkQjIx6sT9E/rUmvITdPrU 3A+/FHYPBUJ/EfvmH3iEJkGeWSj8vCEy3mWnfZ/UCXIlhjtLIwJ9akt9OM9vt90VZTYC QqS/lDPTibcH7TYBJoCjc/A5o9apRescyAA1JXgV15MscBYFsiMGe6ILHxS7eR3CTvmB FXyg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id jx11-20020a170903138b00b001bd9d2e20absi2455522plb.230.2023.09.14.18.54.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 18:54:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 6B18C858DB40; Thu, 14 Sep 2023 18:51:07 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231634AbjIOBvA (ORCPT + 99 others); Thu, 14 Sep 2023 21:51:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231635AbjIOBuo (ORCPT ); Thu, 14 Sep 2023 21:50:44 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1738E30EA; Thu, 14 Sep 2023 18:50:11 -0700 (PDT) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8AxlPBJuANlNf8nAA--.11729S3; Fri, 15 Sep 2023 09:50:01 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Axndw9uANl+ioGAA--.11927S21; Fri, 15 Sep 2023 09:50:00 +0800 (CST) From: Tianrui Zhao To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Huacai Chen , WANG Xuerui , Greg Kroah-Hartman , loongarch@lists.linux.dev, Jens Axboe , Mark Brown , Alex Deucher , Oliver Upton , maobibo@loongson.cn, Xi Ruoyao , zhaotianrui@loongson.cn Subject: [PATCH v21 19/29] LoongArch: KVM: Implement handle csr exception Date: Fri, 15 Sep 2023 09:49:39 +0800 Message-Id: <20230915014949.1222777-20-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230915014949.1222777-1-zhaotianrui@loongson.cn> References: <20230915014949.1222777-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: AQAAf8Axndw9uANl+ioGAA--.11927S21 X-CM-SenderInfo: p2kd03xldq233l6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Thu, 14 Sep 2023 18:51:07 -0700 (PDT) Implement kvm handle LoongArch vcpu exit caused by reading and writing csr. Using csr structure to emulate the registers. Reviewed-by: Bibo Mao Signed-off-by: Tianrui Zhao --- arch/loongarch/kvm/exit.c | 109 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 arch/loongarch/kvm/exit.c diff --git a/arch/loongarch/kvm/exit.c b/arch/loongarch/kvm/exit.c new file mode 100644 index 0000000000..f77175351b --- /dev/null +++ b/arch/loongarch/kvm/exit.c @@ -0,0 +1,109 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2020-2023 Loongson Technology Corporation Limited + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "trace.h" + +static unsigned long kvm_emu_read_csr(struct kvm_vcpu *vcpu, int csrid) +{ + struct loongarch_csrs *csr = vcpu->arch.csr; + unsigned long val = 0; + + /* + * From LoongArch Reference Manual Volume 1 Chapter 4.2.1 + * For undefined csr id, return value is 0 + */ + if (get_gcsr_flag(csrid) & SW_GCSR) + val = kvm_read_sw_gcsr(csr, csrid); + else + pr_warn_once("Unsupport csrread 0x%x with pc %lx\n", + csrid, vcpu->arch.pc); + return val; +} + +static unsigned long kvm_emu_write_csr(struct kvm_vcpu *vcpu, int csrid, + unsigned long val) +{ + struct loongarch_csrs *csr = vcpu->arch.csr; + unsigned long old = 0; + + if (get_gcsr_flag(csrid) & SW_GCSR) { + old = kvm_read_sw_gcsr(csr, csrid); + kvm_write_sw_gcsr(csr, csrid, val); + } else + pr_warn_once("Unsupport csrwrite 0x%x with pc %lx\n", + csrid, vcpu->arch.pc); + + return old; +} + +static unsigned long kvm_emu_xchg_csr(struct kvm_vcpu *vcpu, int csrid, + unsigned long csr_mask, unsigned long val) +{ + struct loongarch_csrs *csr = vcpu->arch.csr; + unsigned long old = 0; + + if (get_gcsr_flag(csrid) & SW_GCSR) { + old = kvm_read_sw_gcsr(csr, csrid); + val = (old & ~csr_mask) | (val & csr_mask); + kvm_write_sw_gcsr(csr, csrid, val); + old = old & csr_mask; + } else + pr_warn_once("Unsupport csrxchg 0x%x with pc %lx\n", + csrid, vcpu->arch.pc); + + return old; +} + +static int kvm_handle_csr(struct kvm_vcpu *vcpu, larch_inst inst) +{ + unsigned int rd, rj, csrid; + unsigned long csr_mask; + unsigned long val = 0; + + /* + * CSR value mask imm + * rj = 0 means csrrd + * rj = 1 means csrwr + * rj != 0,1 means csrxchg + */ + rd = inst.reg2csr_format.rd; + rj = inst.reg2csr_format.rj; + csrid = inst.reg2csr_format.csr; + + /* Process CSR ops */ + if (rj == 0) { + /* process csrrd */ + val = kvm_emu_read_csr(vcpu, csrid); + vcpu->arch.gprs[rd] = val; + } else if (rj == 1) { + /* process csrwr */ + val = vcpu->arch.gprs[rd]; + val = kvm_emu_write_csr(vcpu, csrid, val); + vcpu->arch.gprs[rd] = val; + } else { + /* process csrxchg */ + val = vcpu->arch.gprs[rd]; + csr_mask = vcpu->arch.gprs[rj]; + val = kvm_emu_xchg_csr(vcpu, csrid, csr_mask, val); + vcpu->arch.gprs[rd] = val; + } + + return EMULATE_DONE; +} -- 2.39.1