From patchwork Tue Feb 28 07:00:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: zhaotianrui X-Patchwork-Id: 62290 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp2857423wrd; Mon, 27 Feb 2023 23:02:03 -0800 (PST) X-Google-Smtp-Source: AK7set/DmgqW2St+p5wJE+s0c+Bx162TvghytaWHbskMCToWCtFpiFf0bcK/zE07sYakcEl2/o/+ X-Received: by 2002:a17:903:189:b0:19c:df6e:6aec with SMTP id z9-20020a170903018900b0019cdf6e6aecmr12794377plg.7.1677567723211; Mon, 27 Feb 2023 23:02:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677567723; cv=none; d=google.com; s=arc-20160816; b=YVYEGN97xcY+DXqZtDi12HUAt9H2XM+4ghTCcxmdLFYlGxa/em9ZDc0U3jwx900NEH 4Yd/H2G30+05d0SulXaZlH2sdWzOEyVa75E4j3J3brSzi0nc/LTLyAgb5tn/9u6GrIQS JjCjNm5Xk1DcX0u39FtcxDoZyFZKb4nyNC6x7uvxIeEEWaj3u4HY4lK5fex+QQNoLuIP lWMRYFIO4yGu0Pw8M+ZyobzwO4CUK50ePJF4phtUi7CuwN+ESQ88k27v0i4GmKr4Mkse /Wt5dpqnHhWc7xK5kHxJzJnvEruIrJHaHvvPwliyzYintSmNsEhxFyfQx8mYDDq4VV8/ +W7g== 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=3WklLHSjyuRM2TRl+pnpMr5q7sKH42PUC+qJoPGC2OM=; b=kpkQFWHSoVl+Y1i2ElGyCLj8sZ0sGpHdJnzlr/W/EIyCLiX+TWBN3F/Ks9zQodvry2 g5X7S7HzzKjql7z7P5/d5RyYbFp0P4XErIU+4oJU2YihSl+q229LETVafjpjz6+RUjtn V/HNGrvgchnI1OScTBegecSGAWp50X3yuewojOc8k8zN2yGWp2S2LKjZYo2zBW2BNq8r nRQneEFC5OWOGZRWjhByLo1IF781KQRAmCZSBwVSQ663dPV5rSTtJKLcJNZgsxYnv4Dg uZC4a2JO8ADwgugSy20LoxBNyixtE0JTZqPiqGq649tU/ovgg+03m2oi4PUML3Q1YcHs dSTQ== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i21-20020a632215000000b00502d6f14309si8551979pgi.809.2023.02.27.23.01.50; Mon, 27 Feb 2023 23:02:03 -0800 (PST) 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 S230328AbjB1HB1 (ORCPT + 99 others); Tue, 28 Feb 2023 02:01:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229800AbjB1HBE (ORCPT ); Tue, 28 Feb 2023 02:01:04 -0500 Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id CA9DDFF06; Mon, 27 Feb 2023 23:01:01 -0800 (PST) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8DxAf+spv1jbmAGAA--.6583S3; Tue, 28 Feb 2023 15:01:00 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Bxc+Wppv1jPG9AAA--.15462S10; Tue, 28 Feb 2023 15:01:00 +0800 (CST) From: Tianrui Zhao To: Paolo Bonzini Cc: Huacai Chen , WANG Xuerui , Greg Kroah-Hartman , loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Jens Axboe , Mark Brown , Alex Deucher , Oliver Upton , maobibo@loongson.cn, Xi Ruoyao Subject: [PATCH v3 08/29] LoongArch: KVM: Implement vcpu handle exit interface Date: Tue, 28 Feb 2023 15:00:35 +0800 Message-Id: <20230228070057.3687180-9-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230228070057.3687180-1-zhaotianrui@loongson.cn> References: <20230228070057.3687180-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAf8Bxc+Wppv1jPG9AAA--.15462S10 X-CM-SenderInfo: p2kd03xldq233l6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBjvJXoW7WF17ArW7XFyfGry7XrW7XFb_yoW8CF1rpF 4kCr90gr4rG34xJwnayr4qqr4Yq3s7Kr17ZrZrX3y2yFsFvas8Xr4kKrZrtFyrWw1FqF1f Xr1rG3WY9F4jywUanT9S1TB71UUUUjJqnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj qI5I8CrVACY4xI64kE6c02F40Ex7xfYxn0WfASr-VFAUDa7-sFnT9fnUUIcSsGvfJTRUUU b4kFc2x0x2IEx4CE42xK8VAvwI8IcIk0rVWrJVCq3wA2ocxC64kIII0Yj41l84x0c7CEw4 AK67xGY2AK021l84ACjcxK6xIIjxv20xvE14v26w1j6s0DM28EF7xvwVC0I7IYx2IY6xkF 7I0E14v26r4UJVWxJr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aV CY1x0267AKxVW8Jr0_Cr1UM2kKe7AKxVWUAVWUtwAS0I0E0xvYzxvE52x082IY62kv0487 Mc804VCY07AIYIkI8VC2zVCFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VCjz48v1s IEY20_WwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lc7CjxVAaw2AFwI0_ JF0_Jw1l42xK82IYc2Ij64vIr41l42xK82IY6x8ErcxFaVAv8VWrMxC20s026xCaFVCjc4 AY6r1j6r4UMxCIbckI1I0E14v26r126r1DMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCj r7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6x IIjxv20xvE14v26w1j6s0DMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWxJVW8Jr1lIxAIcVCF 04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26F4j6r4UJwCI42IY6I8E87Iv6x kF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvj4RKpBTUUUUU X-Spam-Status: No, score=1.4 required=5.0 tests=BAYES_00,RCVD_IN_SBL_CSS, SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: * 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759057253165408216?= X-GMAIL-MSGID: =?utf-8?q?1759057253165408216?= 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 | 46 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c index 4fcfb36a7133..e16b7ca852e3 100644 --- a/arch/loongarch/kvm/vcpu.c +++ b/arch/loongarch/kvm/vcpu.c @@ -54,6 +54,52 @@ static void kvm_pre_enter_guest(struct kvm_vcpu *vcpu) vcpu->arch.host_eentry = csr_read64(LOONGARCH_CSR_EENTRY); } +/* + * 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; + run->ready_for_interrupt_injection = 1; + + local_irq_enable(); + guest_state_exit_irqoff(); + + trace_kvm_exit(vcpu, exccode); + if (exccode) { + ret = _kvm_handle_fault(vcpu, exccode); + } else { + WARN(!intr, "suspicious vm exiting"); + ++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;