Re: [PATCH 1/2] i386/xen: Move KVM_XEN_HVM_CONFIG ioctl to kvm_xen_init_vcpu()

2025-04-05 Thread David Woodhouse
On Fri, 2025-02-07 at 14:37 +, David Woodhouse wrote:
> From: David Woodhouse 
> 
> At the time kvm_xen_init() is called, hyperv_enabled() doesn't yet work, so
> the correct MSR index to use for the hypercall page isn't known.
> 
> Rather than setting it to the default and then shifting it later for the
> Hyper-V case with a confusing second call to kvm_init_xen(), just do it
> once in kvm_xen_init_vcpu().
> 
> Signed-off-by: David Woodhouse 

Ping?


smime.p7s
Description: S/MIME cryptographic signature


Re: [PATCH 1/2] i386/xen: Move KVM_XEN_HVM_CONFIG ioctl to kvm_xen_init_vcpu()

2025-02-07 Thread David Woodhouse
On 7 February 2025 15:37:40 GMT, Sean Christopherson  wrote:
>On Fri, Feb 07, 2025, David Woodhouse wrote:
>> From: David Woodhouse 
>> 
>> At the time kvm_xen_init() is called, hyperv_enabled() doesn't yet work, so
>> the correct MSR index to use for the hypercall page isn't known.
>> 
>> Rather than setting it to the default and then shifting it later for the
>> Hyper-V case with a confusing second call to kvm_init_xen(), just do it
>> once in kvm_xen_init_vcpu().
>
>Is it possible the funky double-init is deliberate, to ensure that Xen is
>configured in KVM during VM setup?  I looked through KVM and didn't see any
>obvious dependencies, but that doesn't mean a whole lot.

I am fairly sure there are no such dependencies. It was just this way because 
shifting the MSR to accommodate Hyper-V (and making kvm_xen_init() idempotent 
in order to do so) was an afterthought. In retrospect, I should have done it 
this way from the start. It's cleaner. And you don't require as much caffeine 
to understand it :)



Re: [PATCH 1/2] i386/xen: Move KVM_XEN_HVM_CONFIG ioctl to kvm_xen_init_vcpu()

2025-02-07 Thread Sean Christopherson
On Fri, Feb 07, 2025, David Woodhouse wrote:
> From: David Woodhouse 
> 
> At the time kvm_xen_init() is called, hyperv_enabled() doesn't yet work, so
> the correct MSR index to use for the hypercall page isn't known.
> 
> Rather than setting it to the default and then shifting it later for the
> Hyper-V case with a confusing second call to kvm_init_xen(), just do it
> once in kvm_xen_init_vcpu().

Is it possible the funky double-init is deliberate, to ensure that Xen is
configured in KVM during VM setup?  I looked through KVM and didn't see any
obvious dependencies, but that doesn't mean a whole lot.



[PATCH 1/2] i386/xen: Move KVM_XEN_HVM_CONFIG ioctl to kvm_xen_init_vcpu()

2025-02-07 Thread David Woodhouse
From: David Woodhouse 

At the time kvm_xen_init() is called, hyperv_enabled() doesn't yet work, so
the correct MSR index to use for the hypercall page isn't known.

Rather than setting it to the default and then shifting it later for the
Hyper-V case with a confusing second call to kvm_init_xen(), just do it
once in kvm_xen_init_vcpu().

Signed-off-by: David Woodhouse 
---
 target/i386/kvm/kvm.c | 16 +-
 target/i386/kvm/xen-emu.c | 44 ---
 target/i386/kvm/xen-emu.h |  4 ++--
 3 files changed, 30 insertions(+), 34 deletions(-)

diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
index 6c749d4ee8..b4deec6255 100644
--- a/target/i386/kvm/kvm.c
+++ b/target/i386/kvm/kvm.c
@@ -2129,6 +2129,8 @@ int kvm_arch_init_vcpu(CPUState *cs)
 if (cs->kvm_state->xen_version) {
 #ifdef CONFIG_XEN_EMU
 struct kvm_cpuid_entry2 *xen_max_leaf;
+uint32_t hypercall_msr =
+hyperv_enabled(cpu) ? XEN_HYPERCALL_MSR_HYPERV : XEN_HYPERCALL_MSR;
 
 memcpy(signature, "XenVMMXenVMM", 12);
 
@@ -2150,13 +2152,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
 c->function = kvm_base + XEN_CPUID_HVM_MSR;
 /* Number of hypercall-transfer pages */
 c->eax = 1;
-/* Hypercall MSR base address */
-if (hyperv_enabled(cpu)) {
-c->ebx = XEN_HYPERCALL_MSR_HYPERV;
-kvm_xen_init(cs->kvm_state, c->ebx);
-} else {
-c->ebx = XEN_HYPERCALL_MSR;
-}
+c->ebx = hypercall_msr;
 c->ecx = 0;
 c->edx = 0;
 
@@ -2194,7 +2190,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
 }
 }
 
-r = kvm_xen_init_vcpu(cs);
+r = kvm_xen_init_vcpu(cs, hypercall_msr);
 if (r) {
 return r;
 }
@@ -3245,9 +3241,7 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
 error_report("kvm: Xen support only available in PC machine");
 return -ENOTSUP;
 }
-/* hyperv_enabled() doesn't work yet. */
-uint32_t msr = XEN_HYPERCALL_MSR;
-ret = kvm_xen_init(s, msr);
+ret = kvm_xen_init(s);
 if (ret < 0) {
 return ret;
 }
diff --git a/target/i386/kvm/xen-emu.c b/target/i386/kvm/xen-emu.c
index e81a245881..1144a6efcd 100644
--- a/target/i386/kvm/xen-emu.c
+++ b/target/i386/kvm/xen-emu.c
@@ -108,15 +108,11 @@ static inline int kvm_copy_to_gva(CPUState *cs, uint64_t 
gva, void *buf,
 return kvm_gva_rw(cs, gva, buf, sz, true);
 }
 
-int kvm_xen_init(KVMState *s, uint32_t hypercall_msr)
+int kvm_xen_init(KVMState *s)
 {
 const int required_caps = KVM_XEN_HVM_CONFIG_HYPERCALL_MSR |
 KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL | KVM_XEN_HVM_CONFIG_SHARED_INFO;
-struct kvm_xen_hvm_config cfg = {
-.msr = hypercall_msr,
-.flags = KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL,
-};
-int xen_caps, ret;
+int xen_caps;
 
 xen_caps = kvm_check_extension(s, KVM_CAP_XEN_HVM);
 if (required_caps & ~xen_caps) {
@@ -130,20 +126,6 @@ int kvm_xen_init(KVMState *s, uint32_t hypercall_msr)
 .u.xen_version = s->xen_version,
 };
 (void)kvm_vm_ioctl(s, KVM_XEN_HVM_SET_ATTR, &ha);
-
-cfg.flags |= KVM_XEN_HVM_CONFIG_EVTCHN_SEND;
-}
-
-ret = kvm_vm_ioctl(s, KVM_XEN_HVM_CONFIG, &cfg);
-if (ret < 0) {
-error_report("kvm: Failed to enable Xen HVM support: %s",
- strerror(-ret));
-return ret;
-}
-
-/* If called a second time, don't repeat the rest of the setup. */
-if (s->xen_caps) {
-return 0;
 }
 
 /*
@@ -185,10 +167,14 @@ int kvm_xen_init(KVMState *s, uint32_t hypercall_msr)
 return 0;
 }
 
-int kvm_xen_init_vcpu(CPUState *cs)
+int kvm_xen_init_vcpu(CPUState *cs, uint32_t hypercall_msr)
 {
 X86CPU *cpu = X86_CPU(cs);
 CPUX86State *env = &cpu->env;
+struct kvm_xen_hvm_config cfg = {
+.msr = hypercall_msr,
+.flags = KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL,
+};
 int err;
 
 /*
@@ -210,6 +196,22 @@ int kvm_xen_init_vcpu(CPUState *cs)
  strerror(-err));
 return err;
 }
+
+cfg.flags |= KVM_XEN_HVM_CONFIG_EVTCHN_SEND;
+}
+
+/*
+ * This is a per-KVM setting, but hyperv_enabled() can't be used
+ * when kvm_xen_init() is called from kvm_arch_init(), so do it
+ * when the BSP is initialized.
+ */
+if (cs->cpu_index == 0) {
+err = kvm_vm_ioctl(cs->kvm_state, KVM_XEN_HVM_CONFIG, &cfg);
+if (err) {
+error_report("kvm: Failed to enable Xen HVM support: %s",
+ strerror(-err));
+return err;
+}
 }
 
 env->xen_vcpu_info_gpa = INVALID_GPA;
diff --git a/target/i386/kvm/xen-emu.h b/target/i386/kvm/xen-emu.h
index fe85e0b195..7a7c72eee5 100644
--- a/target/i386/kvm/xen-emu.h
+++ b/target/i386/kvm/xen-emu.h
@@ -23,8 +23,8 @@
 
 #define XEN_VERSION(maj, min) ((maj)