From nobody Sat Nov 23 19:59:12 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1729763163560248.74633074995893; Thu, 24 Oct 2024 02:46:03 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t3uOj-00044n-Lo; Thu, 24 Oct 2024 05:44:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1t3uOi-00044K-Kb for qemu-devel@nongnu.org; Thu, 24 Oct 2024 05:44:52 -0400 Received: from mail.loongson.cn ([114.242.206.163]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t3uOf-0002ZF-9T for qemu-devel@nongnu.org; Thu, 24 Oct 2024 05:44:52 -0400 Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8AxaeALFxpnIG4KAA--.3561S3; Thu, 24 Oct 2024 17:44:43 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by front1 (Coremail) with SMTP id qMiowMCxPMICFxpnHi0PAA--.1186S8; Thu, 24 Oct 2024 17:44:42 +0800 (CST) From: Song Gao To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, Bibo Mao Subject: [PULL 6/6] target/loongarch: Add steal time support on migration Date: Thu, 24 Oct 2024 17:26:25 +0800 Message-Id: <20241024092626.1328049-7-gaosong@loongson.cn> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20241024092626.1328049-1-gaosong@loongson.cn> References: <20241024092626.1328049-1-gaosong@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qMiowMCxPMICFxpnHi0PAA--.1186S8 X-CM-SenderInfo: 5jdr20tqj6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=114.242.206.163; envelope-from=gaosong@loongson.cn; helo=mail.loongson.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1729763164228116600 Content-Type: text/plain; charset="utf-8" From: Bibo Mao With pv steal time supported, VM machine needs get physical address of each vcpu and notify new host during migration. Here two functions kvm_get_stealtime/kvm_set_stealtime, and guest steal time physical address is only updated on KVM_PUT_FULL_STATE stage. Signed-off-by: Bibo Mao Reviewed-by: Song Gao Message-Id: <20240930064040.753929-1-maobibo@loongson.cn> Signed-off-by: Song Gao --- target/loongarch/cpu.h | 3 ++ target/loongarch/kvm/kvm.c | 65 ++++++++++++++++++++++++++++++++++++++ target/loongarch/machine.c | 6 ++-- 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/target/loongarch/cpu.h b/target/loongarch/cpu.h index 95be58dd66..86c86c6c95 100644 --- a/target/loongarch/cpu.h +++ b/target/loongarch/cpu.h @@ -364,6 +364,9 @@ typedef struct CPUArchState { uint64_t CSR_DBG; uint64_t CSR_DERA; uint64_t CSR_DSAVE; + struct { + uint64_t guest_addr; + } stealtime; =20 #ifdef CONFIG_TCG float_status fp_status; diff --git a/target/loongarch/kvm/kvm.c b/target/loongarch/kvm/kvm.c index 8bda8ae540..ff81806ca3 100644 --- a/target/loongarch/kvm/kvm.c +++ b/target/loongarch/kvm/kvm.c @@ -34,6 +34,55 @@ const KVMCapabilityInfo kvm_arch_required_capabilities[]= =3D { KVM_CAP_LAST_INFO }; =20 +static int kvm_get_stealtime(CPUState *cs) +{ + CPULoongArchState *env =3D cpu_env(cs); + int err; + struct kvm_device_attr attr =3D { + .group =3D KVM_LOONGARCH_VCPU_PVTIME_CTRL, + .attr =3D KVM_LOONGARCH_VCPU_PVTIME_GPA, + .addr =3D (uint64_t)&env->stealtime.guest_addr, + }; + + err =3D kvm_vcpu_ioctl(cs, KVM_HAS_DEVICE_ATTR, attr); + if (err) { + return 0; + } + + err =3D kvm_vcpu_ioctl(cs, KVM_GET_DEVICE_ATTR, attr); + if (err) { + error_report("PVTIME: KVM_GET_DEVICE_ATTR: %s", strerror(errno)); + return err; + } + + return 0; +} + +static int kvm_set_stealtime(CPUState *cs) +{ + CPULoongArchState *env =3D cpu_env(cs); + int err; + struct kvm_device_attr attr =3D { + .group =3D KVM_LOONGARCH_VCPU_PVTIME_CTRL, + .attr =3D KVM_LOONGARCH_VCPU_PVTIME_GPA, + .addr =3D (uint64_t)&env->stealtime.guest_addr, + }; + + err =3D kvm_vcpu_ioctl(cs, KVM_HAS_DEVICE_ATTR, attr); + if (err) { + return 0; + } + + err =3D kvm_vcpu_ioctl(cs, KVM_SET_DEVICE_ATTR, attr); + if (err) { + error_report("PVTIME: KVM_SET_DEVICE_ATTR %s with gpa "TARGET_FMT_= lx, + strerror(errno), env->stealtime.guest_addr); + return err; + } + + return 0; +} + static int kvm_loongarch_get_regs_core(CPUState *cs) { int ret =3D 0; @@ -670,6 +719,11 @@ int kvm_arch_get_registers(CPUState *cs, Error **errp) return ret; } =20 + ret =3D kvm_get_stealtime(cs); + if (ret) { + return ret; + } + ret =3D kvm_loongarch_get_mpstate(cs); return ret; } @@ -703,6 +757,17 @@ int kvm_arch_put_registers(CPUState *cs, int level, Er= ror **errp) return ret; } =20 + if (level >=3D KVM_PUT_FULL_STATE) { + /* + * only KVM_PUT_FULL_STATE is required, kvm kernel will clear + * guest_addr for KVM_PUT_RESET_STATE + */ + ret =3D kvm_set_stealtime(cs); + if (ret) { + return ret; + } + } + ret =3D kvm_loongarch_put_mpstate(cs); return ret; } diff --git a/target/loongarch/machine.c b/target/loongarch/machine.c index 3d5c84ae9c..efb20e2fbe 100644 --- a/target/loongarch/machine.c +++ b/target/loongarch/machine.c @@ -168,8 +168,8 @@ static const VMStateDescription vmstate_tlb =3D { /* LoongArch CPU state */ const VMStateDescription vmstate_loongarch_cpu =3D { .name =3D "cpu", - .version_id =3D 2, - .minimum_version_id =3D 2, + .version_id =3D 3, + .minimum_version_id =3D 3, .fields =3D (const VMStateField[]) { VMSTATE_UINTTL_ARRAY(env.gpr, LoongArchCPU, 32), VMSTATE_UINTTL(env.pc, LoongArchCPU), @@ -232,6 +232,8 @@ const VMStateDescription vmstate_loongarch_cpu =3D { VMSTATE_UINT64(env.CSR_DSAVE, LoongArchCPU), =20 VMSTATE_UINT64(kvm_state_counter, LoongArchCPU), + /* PV steal time */ + VMSTATE_UINT64(env.stealtime.guest_addr, LoongArchCPU), =20 VMSTATE_END_OF_LIST() }, --=20 2.34.1