KVM Archive mirror
 help / color / mirror / Atom feed
From: Bibo Mao <maobibo@loongson.cn>
To: Tianrui Zhao <zhaotianrui@loongson.cn>,
	Huacai Chen <chenhuacai@kernel.org>
Cc: WANG Xuerui <kernel@xen0n.name>,
	kvm@vger.kernel.org, loongarch@lists.linux.dev,
	linux-kernel@vger.kernel.org
Subject: [PATCH 3/3] LoongArch: KVM: Add vm migration support for LBT feature
Date: Mon, 13 May 2024 09:12:35 +0800	[thread overview]
Message-ID: <20240513011235.3233776-4-maobibo@loongson.cn> (raw)
In-Reply-To: <20240513011235.3233776-1-maobibo@loongson.cn>

Every vcpu has separate LBT registers. And there are four scr registers,
one flags and ftop register for LBT extension. When VM migrates, VMM
needs to get LBT registers for every vcpu.

Here macro KVM_LOONGARCH_VCPU_LBT is added for vcpu attr control info,
the following macro is added to get/put LBT registers.
  KVM_LOONGARCH_VCPU_LBT_SCR0
  KVM_LOONGARCH_VCPU_LBT_SCR1
  KVM_LOONGARCH_VCPU_LBT_SCR2
  KVM_LOONGARCH_VCPU_LBT_SCR3
  KVM_LOONGARCH_VCPU_LBT_FLAGS
  KVM_LOONGARCH_VCPU_LBT_FTOP

Signed-off-by: Bibo Mao <maobibo@loongson.cn>
---
 arch/loongarch/include/uapi/asm/kvm.h |   7 ++
 arch/loongarch/kvm/vcpu.c             | 103 ++++++++++++++++++++++++++
 2 files changed, 110 insertions(+)

diff --git a/arch/loongarch/include/uapi/asm/kvm.h b/arch/loongarch/include/uapi/asm/kvm.h
index 286b5ce93a57..9c3de257fddf 100644
--- a/arch/loongarch/include/uapi/asm/kvm.h
+++ b/arch/loongarch/include/uapi/asm/kvm.h
@@ -85,6 +85,13 @@ struct kvm_fpu {
 #define KVM_LOONGARCH_VCPU_CPUCFG	0
 #define KVM_LOONGARCH_VCPU_PVTIME_CTRL	1
 #define  KVM_LOONGARCH_VCPU_PVTIME_GPA	0
+#define KVM_LOONGARCH_VCPU_LBT		2
+#define  KVM_LOONGARCH_VCPU_LBT_SCR0	0
+#define  KVM_LOONGARCH_VCPU_LBT_SCR1	1
+#define  KVM_LOONGARCH_VCPU_LBT_SCR2	2
+#define  KVM_LOONGARCH_VCPU_LBT_SCR3	3
+#define  KVM_LOONGARCH_VCPU_LBT_FLAGS	4
+#define  KVM_LOONGARCH_VCPU_LBT_FTOP	5
 
 struct kvm_debug_exit_arch {
 };
diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c
index b2856539368a..a84c9d527d9d 100644
--- a/arch/loongarch/kvm/vcpu.c
+++ b/arch/loongarch/kvm/vcpu.c
@@ -765,6 +765,100 @@ static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
 	return -EINVAL;
 }
 
+static int kvm_loongarch_lbt_has_attr(struct kvm_vcpu *vcpu,
+					struct kvm_device_attr *attr)
+{
+	if (!kvm_guest_has_lbt(&vcpu->arch))
+		return -ENXIO;
+
+	switch (attr->attr) {
+	case KVM_LOONGARCH_VCPU_LBT_SCR0:
+	case KVM_LOONGARCH_VCPU_LBT_SCR1:
+	case KVM_LOONGARCH_VCPU_LBT_SCR2:
+	case KVM_LOONGARCH_VCPU_LBT_SCR3:
+	case KVM_LOONGARCH_VCPU_LBT_FLAGS:
+	case KVM_LOONGARCH_VCPU_LBT_FTOP:
+		return 0;
+	default:
+		return -ENXIO;
+	}
+
+	return -ENXIO;
+}
+
+static int kvm_loongarch_lbt_get_attr(struct kvm_vcpu *vcpu,
+					struct kvm_device_attr *attr)
+{
+	uint64_t val;
+
+	if (!kvm_guest_has_lbt(&vcpu->arch))
+		return -ENXIO;
+
+	switch (attr->attr) {
+	case KVM_LOONGARCH_VCPU_LBT_SCR0:
+		val = vcpu->arch.lbt.scr0;
+		break;
+	case KVM_LOONGARCH_VCPU_LBT_SCR1:
+		val = vcpu->arch.lbt.scr1;
+		break;
+	case KVM_LOONGARCH_VCPU_LBT_SCR2:
+		val = vcpu->arch.lbt.scr2;
+		break;
+	case KVM_LOONGARCH_VCPU_LBT_SCR3:
+		val = vcpu->arch.lbt.scr3;
+		break;
+	case KVM_LOONGARCH_VCPU_LBT_FLAGS:
+		val = vcpu->arch.lbt.eflags;
+		break;
+	case KVM_LOONGARCH_VCPU_LBT_FTOP:
+		val = vcpu->arch.fpu.ftop;
+		break;
+	default:
+		return -ENXIO;
+	}
+
+	if (put_user(val, (uint64_t __user *)attr->addr))
+		return -EFAULT;
+	return 0;
+}
+
+static int kvm_loongarch_lbt_set_attr(struct kvm_vcpu *vcpu,
+					struct kvm_device_attr *attr)
+{
+	u64 val;
+
+	if (!kvm_guest_has_lbt(&vcpu->arch))
+		return -ENXIO;
+
+	if (get_user(val, (u64 __user *)attr->addr))
+		return -EFAULT;
+
+	switch (attr->attr) {
+	case KVM_LOONGARCH_VCPU_LBT_SCR0:
+		vcpu->arch.lbt.scr0 = val;
+		break;
+	case KVM_LOONGARCH_VCPU_LBT_SCR1:
+		vcpu->arch.lbt.scr1 = val;
+		break;
+	case KVM_LOONGARCH_VCPU_LBT_SCR2:
+		vcpu->arch.lbt.scr2 = val;
+		break;
+	case KVM_LOONGARCH_VCPU_LBT_SCR3:
+		vcpu->arch.lbt.scr3 = val;
+		break;
+	case KVM_LOONGARCH_VCPU_LBT_FLAGS:
+		vcpu->arch.lbt.eflags = val;
+		break;
+	case KVM_LOONGARCH_VCPU_LBT_FTOP:
+		vcpu->arch.fpu.ftop = val;
+		break;
+	default:
+		return -ENXIO;
+	}
+
+	return 0;
+}
+
 static int kvm_loongarch_cpucfg_has_attr(struct kvm_vcpu *vcpu,
 					 struct kvm_device_attr *attr)
 {
@@ -790,6 +884,9 @@ static int kvm_loongarch_vcpu_has_attr(struct kvm_vcpu *vcpu,
 	case KVM_LOONGARCH_VCPU_PVTIME_CTRL:
 		ret = kvm_loongarch_pvtime_has_attr(vcpu, attr);
 		break;
+	case KVM_LOONGARCH_VCPU_LBT:
+		ret = kvm_loongarch_lbt_has_attr(vcpu, attr);
+		break;
 	default:
 		break;
 	}
@@ -825,6 +922,9 @@ static int kvm_loongarch_vcpu_get_attr(struct kvm_vcpu *vcpu,
 	case KVM_LOONGARCH_VCPU_PVTIME_CTRL:
 		ret = kvm_loongarch_pvtime_get_attr(vcpu, attr);
 		break;
+	case KVM_LOONGARCH_VCPU_LBT:
+		ret = kvm_loongarch_lbt_get_attr(vcpu, attr);
+		break;
 	default:
 		break;
 	}
@@ -850,6 +950,9 @@ static int kvm_loongarch_vcpu_set_attr(struct kvm_vcpu *vcpu,
 	case KVM_LOONGARCH_VCPU_PVTIME_CTRL:
 		ret = kvm_loongarch_pvtime_set_attr(vcpu, attr);
 		break;
+	case KVM_LOONGARCH_VCPU_LBT:
+		ret = kvm_loongarch_lbt_set_attr(vcpu, attr);
+		break;
 	default:
 		break;
 	}
-- 
2.39.3


      parent reply	other threads:[~2024-05-13  1:12 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-13  1:12 [PATCH 0/3] LoongArch: KVM: Add Binary Translation extension support Bibo Mao
2024-05-13  1:12 ` [PATCH 1/3] LoongArch: KVM: Add HW " Bibo Mao
2024-05-13  1:12 ` [PATCH 2/3] LoongArch: KVM: Add LBT feature detection with cpucfg Bibo Mao
2024-05-13  1:12 ` Bibo Mao [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240513011235.3233776-4-maobibo@loongson.cn \
    --to=maobibo@loongson.cn \
    --cc=chenhuacai@kernel.org \
    --cc=kernel@xen0n.name \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=loongarch@lists.linux.dev \
    --cc=zhaotianrui@loongson.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).