Received: by 2002:a05:6358:700f:b0:131:369:b2a3 with SMTP id 15csp1672227rwo; Wed, 2 Aug 2023 19:31:27 -0700 (PDT) X-Google-Smtp-Source: APBJJlH+5AT8wQV9x9tNGA7pSIh1dhvmdg82XbqN4SXKzo+C7lSQLol8OS044FosjpS/f4h7xMMq X-Received: by 2002:a17:907:7745:b0:993:d536:3cb8 with SMTP id kx5-20020a170907774500b00993d5363cb8mr6389138ejc.2.1691029886889; Wed, 02 Aug 2023 19:31:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691029886; cv=none; d=google.com; s=arc-20160816; b=kY3yp6G9r2xfAb+MNBJwjTbE7+mmwGVMBkw2bS93yYceXX4yh6emRAnVi4f3hOg1OW 9HzeoGqeUCld47YjRCYjJD4l8MumUHJjhgQ6g/U05FAoCPtsqXPaw5m3GTEzIzNtqvZh Q0XF9OM/tTsnBpezDPWtig8e1p5Nb1VujhNbT8/rWymLHuY2YBnjF0rXr4ld2dKROddi bJqE9G+VFXIE/jyJWTjHdJL/PWTipBcAhqlf+Ao1GkWUp3h0EFPPIUjbdbFnMKD+Gjme gttnHxl9aHmbPMvqCQ2/bRQJe6glp3+kysYAk18XmpwymQR+qWmW47HPununQOly7rFX zBbQ== 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=45htidFsgsotObG+nla5/znzmlYkIHCXNZU7rX7AH/g=; fh=vBXozhnPXwSB3DJhjnKWz3f6luaN/KxHDpM6/QeZg4o=; b=UEvnwYdFXCrRve+G2B5I3DWuQOxsHgEOkk1jEqRxLloWf45uPVuW6AUvREW7WysMP2 MrFIw5kQYChKxhLqUipGe+TvZsiSoE8M1WDc37zwR87bI0w5VvZDFDjB+/DW/iPorRlP yka7OVjR6/6qZsPBqFfmfQc7FUXVvqiiGFcPA/Ty2oHMY6OZYlU2w3SL376kGT7JhnHk C/tN58OdoNvB8vat/2pQeg17LCd5CPuo9KC4gnzgavL2fwNjylKMp14m+1djXA4GqIQb 2HwFfR6cOOMddl802utMjwuliFEyIO1CkKx052IPLw5Qrx/3S8lE3D5oHtXSH1d+xcoO 1E6w== 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 gz21-20020a170906f2d500b0098718971f52si11283069ejb.87.2023.08.02.19.31.01; Wed, 02 Aug 2023 19:31:26 -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 S232724AbjHCCWh (ORCPT + 99 others); Wed, 2 Aug 2023 22:22:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230413AbjHCCVz (ORCPT ); Wed, 2 Aug 2023 22:21:55 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3CE9A2688; Wed, 2 Aug 2023 19:21:52 -0700 (PDT) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8Cx5_E+D8tk1GgPAA--.36001S3; Thu, 03 Aug 2023 10:21:50 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Cx_c4yD8tk8oNGAA--.51268S8; Thu, 03 Aug 2023 10:21:47 +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 v18 06/30] LoongArch: KVM: Implement vcpu create and destroy interface Date: Thu, 3 Aug 2023 10:21:14 +0800 Message-Id: <20230803022138.2736430-7-zhaotianrui@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230803022138.2736430-1-zhaotianrui@loongson.cn> References: <20230803022138.2736430-1-zhaotianrui@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: AQAAf8Cx_c4yD8tk8oNGAA--.51268S8 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 create and destroy interface, saving some info into vcpu arch structure such as vcpu exception entrance, vcpu enter guest pointer, etc. Init vcpu timer and set address translation mode when vcpu create. Reviewed-by: Bibo Mao Signed-off-by: Tianrui Zhao --- arch/loongarch/kvm/vcpu.c | 86 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 arch/loongarch/kvm/vcpu.c diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c new file mode 100644 index 000000000000..e5b66aa82083 --- /dev/null +++ b/arch/loongarch/kvm/vcpu.c @@ -0,0 +1,86 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2020-2023 Loongson Technology Corporation Limited + */ + +#include +#include +#include +#include +#include + +#define CREATE_TRACE_POINTS +#include "trace.h" + +int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) +{ + return 0; +} + +int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) +{ + unsigned long timer_hz; + struct loongarch_csrs *csr; + + vcpu->arch.vpid = 0; + + hrtimer_init(&vcpu->arch.swtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED); + vcpu->arch.swtimer.function = kvm_swtimer_wakeup; + + vcpu->arch.guest_eentry = (unsigned long)kvm_loongarch_ops->guest_eentry; + vcpu->arch.handle_exit = _kvm_handle_exit; + vcpu->arch.csr = kzalloc(sizeof(struct loongarch_csrs), GFP_KERNEL); + if (!vcpu->arch.csr) + return -ENOMEM; + + /* + * kvm all exceptions share one exception entry, and host <-> guest switch + * also switch excfg.VS field, keep host excfg.VS info here + */ + vcpu->arch.host_ecfg = (read_csr_ecfg() & CSR_ECFG_VS); + + /* Init */ + vcpu->arch.last_sched_cpu = -1; + + /* + * Initialize guest register state to valid architectural reset state. + */ + timer_hz = calc_const_freq(); + kvm_init_timer(vcpu, timer_hz); + + /* Set Initialize mode for GUEST */ + csr = vcpu->arch.csr; + kvm_write_sw_gcsr(csr, LOONGARCH_CSR_CRMD, CSR_CRMD_DA); + + /* Set cpuid */ + kvm_write_sw_gcsr(csr, LOONGARCH_CSR_TMID, vcpu->vcpu_id); + + /* start with no pending virtual guest interrupts */ + csr->csrs[LOONGARCH_CSR_GINTC] = 0; + + return 0; +} + +void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) +{ +} + +void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) +{ + int cpu; + struct kvm_context *context; + + hrtimer_cancel(&vcpu->arch.swtimer); + kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache); + kfree(vcpu->arch.csr); + + /* + * If the vCPU is freed and reused as another vCPU, we don't want the + * matching pointer wrongly hanging around in last_vcpu. + */ + for_each_possible_cpu(cpu) { + context = per_cpu_ptr(vcpu->kvm->arch.vmcs, cpu); + if (context->last_vcpu == vcpu) + context->last_vcpu = NULL; + } +} -- 2.39.1