Received: by 2002:a05:7412:f584:b0:e2:908c:2ebd with SMTP id eh4csp51674rdb; Fri, 1 Sep 2023 22:56:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEKRInZ+jhqPWefslqUFbZppiJepqKqSvMpU9JF1h9POQCFy95+XCYJisY435lvY5W1LmxA X-Received: by 2002:a92:cc50:0:b0:349:2bb0:c87d with SMTP id t16-20020a92cc50000000b003492bb0c87dmr4647362ilq.32.1693634182026; Fri, 01 Sep 2023 22:56:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693634181; cv=none; d=google.com; s=arc-20160816; b=v5VSUWnx8HwBxIDcF703dB0jrzkKxo3kC8DPpf4aDzp2i/mC9FimwiU63poFYM1BPk iaVjPTJ15MSTp2OTtnFZXF8A/z/F91/rWYhMuf6o0m0GPYSHGy1xA241q2x1JggHBn9X SUffPoO1MDgT4Z0HvxV4dP1nJt9j6OLg2TLoLdtZPPj8UC3OonpSSmE6vzme4JX7pGb/ tWVXmlmaYjdjPTJMFURLDF/g832AS4qZ0QcbgDpM1iqmY5mbu80KpNlV0QvxKMF3OKjX tIPnRydbvsBj7nOK1euRWkWmI7BECKUq/gHNiGYGeDvgtnov5005HexBTdCosstQFce4 jFVA== 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=1TLXMrnl2m0IxUCo7cl5Acvlr0SEC3S9R3aM6KgaH+M=; fh=ejoaVAUUbBXsA/6jM/beuI9xCYtV2osdR9SpI7hjY84=; b=Nufgy0hZaI6q68wSnagj5oOL95C0p1JDIpksqO4F38f+N8dJ8+ysSGFfb+Lu5J1QxM kMCAPPGOYGXsgO2zeHQCCH67twZlFrDAm6h6cdEwzJcPNCtTyutWDarrTaje3hkkh0M+ DiypHzVTyJHUzLMzS5JBh/R8JUxvXI0i9G6AMdnT+bj7H8c0AvE/KSnLlNpO/0FIUyvN Mb5hR42Omwbp37blTZuBvGSTt6bmfRDWCS7UaYFhzh0Np6SG44oeaCczZk2idpnkJ9Xd E5wmI3UVJEwdHH3urV3VOd1II/kJs9sfRoJKzRQPPSCsuSk4R7cpLQOblL4lsMcUlLyN V26A== 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 t23-20020a656097000000b005706961d4desi2320289pgu.318.2023.09.01.22.56.17; Fri, 01 Sep 2023 22:56:21 -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 S240490AbjHaIau (ORCPT + 25 others); Thu, 31 Aug 2023 04:30:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245298AbjHaIai (ORCPT ); Thu, 31 Aug 2023 04:30:38 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1DACD19A; Thu, 31 Aug 2023 01:30:33 -0700 (PDT) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8Cx5_GlT_BkIl4dAA--.60020S3; Thu, 31 Aug 2023 16:30:29 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Ax3c6gT_BkOPVnAA--.55892S9; Thu, 31 Aug 2023 16:30:28 +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 v20 07/30] LoongArch: KVM: Implement vcpu run interface Date: Thu, 31 Aug 2023 16:29:57 +0800 Message-Id: <20230831083020.2187109-8-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230831083020.2187109-1-zhaotianrui@loongson.cn> References: <20230831083020.2187109-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: AQAAf8Ax3c6gT_BkOPVnAA--.55892S9 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 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 run interface, handling mmio, iocsr reading fault and deliver interrupt, lose fpu before vcpu enter guest. Reviewed-by: Bibo Mao Signed-off-by: Tianrui Zhao --- arch/loongarch/kvm/vcpu.c | 130 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c index 545b18cd1c..83f2988ea6 100644 --- a/arch/loongarch/kvm/vcpu.c +++ b/arch/loongarch/kvm/vcpu.c @@ -18,6 +18,91 @@ int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) return 0; } +/* + * _kvm_check_requests - check and handle pending vCPU requests + * + * Return: RESUME_GUEST if we should enter the guest + * RESUME_HOST if we should exit to userspace + */ +static int _kvm_check_requests(struct kvm_vcpu *vcpu) +{ + if (!kvm_request_pending(vcpu)) + return RESUME_GUEST; + + if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) + /* Drop vpid for this vCPU */ + vcpu->arch.vpid = 0; + + if (kvm_dirty_ring_check_request(vcpu)) + return RESUME_HOST; + + return RESUME_GUEST; +} + +/* + * Check and handle pending signal and vCPU requests etc + * Run with irq enabled and preempt enabled + * + * Return: RESUME_GUEST if we should enter the guest + * RESUME_HOST if we should exit to userspace + * < 0 if we should exit to userspace, where the return value + * indicates an error + */ +static int kvm_enter_guest_check(struct kvm_vcpu *vcpu) +{ + int ret; + + /* + * Check conditions before entering the guest + */ + ret = xfer_to_guest_mode_handle_work(vcpu); + if (ret < 0) + return ret; + + ret = _kvm_check_requests(vcpu); + return ret; +} + +/* + * called with irq enabled + * + * Return: RESUME_GUEST if we should enter the guest, and irq disabled + * Others if we should exit to userspace + */ +static int kvm_pre_enter_guest(struct kvm_vcpu *vcpu) +{ + int ret; + + do { + ret = kvm_enter_guest_check(vcpu); + if (ret != RESUME_GUEST) + break; + + /* + * handle vcpu timer, interrupts, check requests and + * check vmid before vcpu enter guest + */ + local_irq_disable(); + kvm_acquire_timer(vcpu); + _kvm_deliver_intr(vcpu); + /* make sure the vcpu mode has been written */ + smp_store_mb(vcpu->mode, IN_GUEST_MODE); + _kvm_check_vmid(vcpu); + vcpu->arch.host_eentry = csr_read64(LOONGARCH_CSR_EENTRY); + /* clear KVM_LARCH_CSR as csr will change when enter guest */ + vcpu->arch.aux_inuse &= ~KVM_LARCH_CSR; + + if (kvm_request_pending(vcpu) || xfer_to_guest_mode_work_pending()) { + /* make sure the vcpu mode has been written */ + smp_store_mb(vcpu->mode, OUTSIDE_GUEST_MODE); + local_irq_enable(); + ret = -EAGAIN; + } + } while (ret != RESUME_GUEST); + + return ret; +} + int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) { unsigned long timer_hz; @@ -85,3 +170,48 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) context->last_vcpu = NULL; } } + +int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) +{ + int r = -EINTR; + struct kvm_run *run = vcpu->run; + + if (vcpu->mmio_needed) { + if (!vcpu->mmio_is_write) + _kvm_complete_mmio_read(vcpu, run); + vcpu->mmio_needed = 0; + } + + if (run->exit_reason == KVM_EXIT_LOONGARCH_IOCSR) { + if (!run->iocsr_io.is_write) + _kvm_complete_iocsr_read(vcpu, run); + } + + /* clear exit_reason */ + run->exit_reason = KVM_EXIT_UNKNOWN; + if (run->immediate_exit) + return r; + + lose_fpu(1); + vcpu_load(vcpu); + kvm_sigset_activate(vcpu); + r = kvm_pre_enter_guest(vcpu); + if (r != RESUME_GUEST) + goto out; + + guest_timing_enter_irqoff(); + guest_state_enter_irqoff(); + trace_kvm_enter(vcpu); + r = kvm_loongarch_ops->enter_guest(run, vcpu); + + trace_kvm_out(vcpu); + /* + * guest exit is already recorded at _kvm_handle_exit + * return val must not be RESUME_GUEST + */ + local_irq_enable(); +out: + kvm_sigset_deactivate(vcpu); + vcpu_put(vcpu); + return r; +} -- 2.27.0