Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1814672rwd; Fri, 9 Jun 2023 02:32:15 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6fp/lhz7ICzMF2QyaWVxSJ6jYwgpv/Zz5ibAdnOl2F73UpPSgW7EHZPfVGXTL2KordlJY2 X-Received: by 2002:a17:902:ced0:b0:1b0:7739:657c with SMTP id d16-20020a170902ced000b001b07739657cmr883355plg.55.1686303135150; Fri, 09 Jun 2023 02:32:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686303135; cv=none; d=google.com; s=arc-20160816; b=CerjO67BsoVwLb4KX2cfBLMRebP7jbepgJ/V/0fqVtMjTiw1fWgOt0/OUDWdMnP9ts 8tgTi0cvGPXg+S14b2uX7l8nEwoWTadkdoolMZNrn1g+wqI/0DUWnEVhI/bA8ffjcxJq yWjo1TTLGaoiKT1zz9VOWDl/l2rYSIv/BFujPyfNKaLMO2m04cUwt3jhX/mX+MMZTlxm KTg62zgl98jq/C7/yq4ItH0Zgcsse9EfG2TWLQU7Jw/Y8URgnQcrHTYz14ETApUQZYe5 FS1udVHEbVwhDySR4alIw/Fgcb3pJXpZVC40/+H9+l58EzhDiWeCko1poeN5vFARAlS8 ofYw== 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=hITzPB2vkpZxFpBbJyN80noRiL3x23Jbtx1XNWyanHI=; b=A0foZs+PmTRKeKo7C7prLRT+34vp82H5PUqDgJSmsItlfOqFvXAP38ZaaJOwoSgcl/ OeWDz/yN6fz8J/qeH7WY96Qd+ve1Mq7kEiXdfjxc5T0T5p1+xtu2oiJSIMZhlaX3X1n5 BZkDNF6URrSMdqBQ9PMjxCXOLkfjvNk3uz5GzP62NeYLjlfLUdQ0VQQwCRYqW5Z9BnwE tcgTlgsUv/AEu03YimTRFlivd8gnkcWCokT+vkHG0Av+WXQaTIKxlXGP1iIy+NA1ACPI G+paKlhtmNA0w3m2qostnL9A8boTgGrXfc8cXMaTUmf97qZXiIkBUrBqh4k4DKzNzo80 IJ/g== 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 t8-20020a170902e84800b001adc4babc70si2598526plg.72.2023.06.09.02.32.03; Fri, 09 Jun 2023 02:32:15 -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 S241482AbjFIJWT (ORCPT + 99 others); Fri, 9 Jun 2023 05:22:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240847AbjFIJVo (ORCPT ); Fri, 9 Jun 2023 05:21:44 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6C2B96A41; Fri, 9 Jun 2023 02:16:25 -0700 (PDT) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8Cx_eoV7IJkJ_0AAA--.3100S3; Fri, 09 Jun 2023 17:08:37 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8DxFOQQ7IJkzHQKAA--.31790S10; Fri, 09 Jun 2023 17:08:37 +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, tangyouling@loongson.cn Subject: [PATCH v13 08/30] LoongArch: KVM: Implement vcpu handle exit interface Date: Fri, 9 Jun 2023 17:08:10 +0800 Message-Id: <20230609090832.2131037-9-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230609090832.2131037-1-zhaotianrui@loongson.cn> References: <20230609090832.2131037-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: AQAAf8DxFOQQ7IJkzHQKAA--.31790S10 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,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. 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 eba5c07b8be3..a45e9d9efe5b 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