Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp9169373rwp; Thu, 20 Jul 2023 00:07:59 -0700 (PDT) X-Google-Smtp-Source: APBJJlF6HvYEeXuUJiehtItGXfzXQ4E9+lNDvkYsUvAx+u/M+f0lH9RhaHpPiAqmEWKCHYDWQ3Gv X-Received: by 2002:a17:907:720b:b0:98e:3cef:68ff with SMTP id dr11-20020a170907720b00b0098e3cef68ffmr4844386ejc.43.1689836879685; Thu, 20 Jul 2023 00:07:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689836879; cv=none; d=google.com; s=arc-20160816; b=G079qA6RniLVN89OixAj/zx8ZZxokMs8BSXEBys71pGTgYiuoW3xacj1YKCw4OARyo SY5lvTqcfLRN52hG2KGrTtbpj0MRPtqhMvkCmyaQJNhcv091TXeGtJmqWKwf8BKnyMPZ wC8YFk9yzFxPEikHZrBAM2F6j+ZVd13wizQRX+o4VkxPV5tArXgnhDeqPtbBDKsahLLc 4Uo5Wlg/GzpX8qMFoh4biB4iD6OJJUW8RmwYHyBKQlmT7MTCEu7O/vVv7/KqDgWye79C l09D7ma3HwfyR8+OWuR3mXx8rvJSMlJfJHFXhjF72W7rCDw6D7qck1Tj1RpajFKpmTrF m3Ow== 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=vgKfINQL5ZlcZHpux0LOSQtvD6l6qdndxLafumZFdIY=; fh=vBXozhnPXwSB3DJhjnKWz3f6luaN/KxHDpM6/QeZg4o=; b=loKClQM7+Fn0bFuOjdEEK1VHhZsgTdeLk+0I9hR5YNCGgjI0dEONWRbzLMXtSIZgdR Eh208RR8JzPzeItZ6ee15PK+CmuJX3f9UnewzEwQkYrrirmBwu4/MG4ogtTmsg/37IIH cATZY79XBphFyTrjar7QHuwq2EE27qTXVEG/h580ruJTHxEQ/1oLG7xCS+e/l19/RZqf h57DOUtcwn5CpQxdUAvzi4Wx1WaNKRFEOs7qBtjIyc2uGR/nwtBsiTjOAtt98wR/I8jW 3UHssUuvXgRawG32e0tNf7G4jDh3CY9cVrQCCBGAxWKk9barQQJ94IvSpxc3eOMwHxY3 w78g== ARC-Authentication-Results: i=1; mx.google.com; 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 d1-20020a1709064c4100b0098286b503e4si254335ejw.1041.2023.07.20.00.07.35; Thu, 20 Jul 2023 00:07:59 -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; 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 S230317AbjGTG2q (ORCPT + 99 others); Thu, 20 Jul 2023 02:28:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230160AbjGTG2Y (ORCPT ); Thu, 20 Jul 2023 02:28:24 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 12FB72110; Wed, 19 Jul 2023 23:28:21 -0700 (PDT) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8CxNvEB1LhkSo0HAA--.19502S3; Thu, 20 Jul 2023 14:28:17 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8CxvM7907hkHU81AA--.16059S10; Thu, 20 Jul 2023 14:28:17 +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 v17 08/30] LoongArch: KVM: Implement vcpu handle exit interface Date: Thu, 20 Jul 2023 14:27:51 +0800 Message-Id: <20230720062813.4126751-9-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230720062813.4126751-1-zhaotianrui@loongson.cn> References: <20230720062813.4126751-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: AQAAf8CxvM7907hkHU81AA--.16059S10 X-CM-SenderInfo: p2kd03xldq233l6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 Implement vcpu handle exit interface, getting the exit code by ESTAT register and using kvm exception vector to handle it. Reviewed-by: Bibo Mao Signed-off-by: Tianrui Zhao --- arch/loongarch/kvm/vcpu.c | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c index 485998d20322..a862b5ac15a0 100644 --- a/arch/loongarch/kvm/vcpu.c +++ b/arch/loongarch/kvm/vcpu.c @@ -52,6 +52,51 @@ static void kvm_pre_enter_guest(struct kvm_vcpu *vcpu) vcpu->arch.aux_inuse &= ~KVM_LARCH_CSR; } +/* + * Return 1 for resume guest and "<= 0" for resume host. + */ +static int _kvm_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu) +{ + unsigned long exst = vcpu->arch.host_estat; + u32 intr = exst & 0x1fff; /* ignore NMI */ + u32 exccode = (exst & CSR_ESTAT_EXC) >> CSR_ESTAT_EXC_SHIFT; + int ret = RESUME_GUEST; + + vcpu->mode = OUTSIDE_GUEST_MODE; + + /* Set a default exit reason */ + run->exit_reason = KVM_EXIT_UNKNOWN; + + local_irq_enable(); + guest_state_exit_irqoff(); + + trace_kvm_exit(vcpu, exccode); + if (exccode) { + ret = _kvm_handle_fault(vcpu, exccode); + } else { + WARN(!intr, "vm exiting with suspicious irq\n"); + ++vcpu->stat.int_exits; + } + + cond_resched(); + local_irq_disable(); + + if (ret == RESUME_HOST) + return ret; + + /* Only check for signals if not already exiting to userspace */ + if (signal_pending(current)) { + vcpu->run->exit_reason = KVM_EXIT_INTR; + ++vcpu->stat.signal_exits; + return -EINTR; + } + + kvm_pre_enter_guest(vcpu); + trace_kvm_reenter(vcpu); + guest_state_enter_irqoff(); + return RESUME_GUEST; +} + int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) { unsigned long timer_hz; -- 2.39.1