From patchwork Mon Jun 26 08:47:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: zhaotianrui X-Patchwork-Id: 112773 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp7335173vqr; Mon, 26 Jun 2023 01:51:41 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6C1cV7Olkx9ZolRUFl9G8/imSsIf1PeKptsAi/5C5FNU6XZK2c4o24hHkuRxFFmFJbWSzw X-Received: by 2002:a05:6358:f15:b0:129:df95:f9a9 with SMTP id b21-20020a0563580f1500b00129df95f9a9mr18137456rwj.2.1687769501353; Mon, 26 Jun 2023 01:51:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687769501; cv=none; d=google.com; s=arc-20160816; b=XFQuetjp1zEHgQLePmPhDIctHb/pnqODmLrkU580OiparmOjFnYSeIkMV1Q8A6gyiB lVMPsNutls0aBek5KjTLLJBglunwszRepKULCNRaLzMTXdZFs/iVZc7aNQdyiwR9oYO9 QxwlZ2wkDnIfRgQDxhEuCzXocZ0/LcUV8Z0yW8eogrUXVtrPBMmZOM/8TGh2KDJoVBn3 BoRyqkRArhU3A7srXrHbE+IvEKXfD/OBVkk1X3dT/Sm9iYe39pyO/y3MA130ReBQHcHI 49etcaiR8nogP1UAi3Cx09DAm+LUO7ntWtyzWxRR9faLSBcXIERCeD+LfGJnJcQW2fma Q6VA== 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=2oC+FnzMRJtKj7y2U3RTiD6RRw8R2+ka4TtURMmeXzs=; fh=FJJE1KfQOXNgUCxk/Gxke8tBG9gBLo3aFI+sNzAMBeU=; b=E7WlzKdQn29lM0tg5zDfBqqdXjlEAYNF+eyXTGHJiSSdEm7OwojyS4d0YzhnX6rlYw d141jwkmGhXBvgaNUI0lWvslYPB2I0rOpGstrJSvD+Y3F0GlnRLSsmO4G6QesTb8871f xfEmJkL1KgFJ7a9LuZL9QhA3W6n4KCD95rzzPMzpDweZ9Ltex6tGBvdjLGBWwU5ZVEgN //ZvDQn7tkIYefab+fnbPHxxV9MXXigL0MWaeIWxI63udOK+va8tQU12tUCExMG9Qqaw 32/z/BvKDLG6Qfar2HuBivQIO7jYPD0UoOWjH+iPTUtGjwLQnsDM8/bAxTpxIYq0R5cD p9Fw== 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 by8-20020a056a00400800b0067a4b6093a8si294525pfb.29.2023.06.26.01.51.29; Mon, 26 Jun 2023 01:51:41 -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 S230397AbjFZIsc (ORCPT + 99 others); Mon, 26 Jun 2023 04:48:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39836 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230310AbjFZIsD (ORCPT ); Mon, 26 Jun 2023 04:48:03 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9E7911AA; Mon, 26 Jun 2023 01:48:01 -0700 (PDT) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8Bx38e+UJlkejQCAA--.3367S3; Mon, 26 Jun 2023 16:47:58 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Bxxsy5UJlk4hQJAA--.19269S9; Mon, 26 Jun 2023 16:47:57 +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 v15 07/30] LoongArch: KVM: Implement vcpu run interface Date: Mon, 26 Jun 2023 16:47:29 +0800 Message-Id: <20230626084752.1138621-8-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230626084752.1138621-1-zhaotianrui@loongson.cn> References: <20230626084752.1138621-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAf8Bxxsy5UJlk4hQJAA--.19269S9 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1769754592517937340?= X-GMAIL-MSGID: =?utf-8?q?1769754592517937340?= 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 | 83 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c index 24b5b00266a1..eba5c07b8be3 100644 --- a/arch/loongarch/kvm/vcpu.c +++ b/arch/loongarch/kvm/vcpu.c @@ -17,6 +17,41 @@ int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) return 0; } +/* Returns 1 if the guest TLB may be clobbered */ +static int _kvm_check_requests(struct kvm_vcpu *vcpu) +{ + int ret = 0; + + if (!kvm_request_pending(vcpu)) + return 0; + + if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) { + /* Drop vpid for this vCPU */ + vcpu->arch.vpid = 0; + /* This will clobber guest TLB contents too */ + ret = 1; + } + + return ret; +} + +static void kvm_pre_enter_guest(struct kvm_vcpu *vcpu) +{ + /* + * handle vcpu timer, interrupts, check requests and + * check vmid before vcpu enter guest + */ + 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_requests(vcpu); + _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; +} + int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) { unsigned long timer_hz; @@ -86,3 +121,51 @@ 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; + + vcpu_load(vcpu); + + kvm_sigset_activate(vcpu); + + 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) + goto out; + + lose_fpu(1); + + local_irq_disable(); + guest_timing_enter_irqoff(); + + kvm_pre_enter_guest(vcpu); + trace_kvm_enter(vcpu); + + guest_state_enter_irqoff(); + r = kvm_loongarch_ops->enter_guest(run, vcpu); + + /* guest_state_exit_irqoff() already done. */ + trace_kvm_out(vcpu); + guest_timing_exit_irqoff(); + local_irq_enable(); + +out: + kvm_sigset_deactivate(vcpu); + + vcpu_put(vcpu); + return r; +}