qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] i386/xen: Move KVM_XEN_HVM_CONFIG ioctl to kvm_xen_init_vcpu()
@ 2025-02-07 14:37 David Woodhouse
  2025-02-07 14:37 ` [PATCH 2/2] hw/xen: Add "mode" parameter to xen-block devices David Woodhouse
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: David Woodhouse @ 2025-02-07 14:37 UTC (permalink / raw)
  To: qemu-devel
  Cc: Stefano Stabellini, Anthony PERARD, Paul Durrant,
	Edgar E. Iglesias, Kevin Wolf, Hanna Reitz, Paolo Bonzini,
	Marcelo Tosatti, David Woodhouse, xen-devel, qemu-block, kvm,
	Sean Christopherson

From: David Woodhouse <dwmw@amazon.co.uk>

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 <dwmw@amazon.co.uk>
---
 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) << 16 | (min))
 
-int kvm_xen_init(KVMState *s, uint32_t hypercall_msr);
-int kvm_xen_init_vcpu(CPUState *cs);
+int kvm_xen_init(KVMState *s);
+int kvm_xen_init_vcpu(CPUState *cs, uint32_t hypercall_msr);
 int kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit);
 int kvm_put_xen_state(CPUState *cs);
 int kvm_get_xen_state(CPUState *cs);
-- 
2.48.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/2] hw/xen: Add "mode" parameter to xen-block devices
  2025-02-07 14:37 [PATCH 1/2] i386/xen: Move KVM_XEN_HVM_CONFIG ioctl to kvm_xen_init_vcpu() David Woodhouse
@ 2025-02-07 14:37 ` David Woodhouse
  2025-03-07 10:38   ` Anthony PERARD
  2025-02-07 15:37 ` [PATCH 1/2] i386/xen: Move KVM_XEN_HVM_CONFIG ioctl to kvm_xen_init_vcpu() Sean Christopherson
  2025-03-29  7:28 ` David Woodhouse
  2 siblings, 1 reply; 6+ messages in thread
From: David Woodhouse @ 2025-02-07 14:37 UTC (permalink / raw)
  To: qemu-devel
  Cc: Stefano Stabellini, Anthony PERARD, Paul Durrant,
	Edgar E. Iglesias, Kevin Wolf, Hanna Reitz, Paolo Bonzini,
	Marcelo Tosatti, David Woodhouse, xen-devel, qemu-block, kvm,
	Sean Christopherson

From: David Woodhouse <dwmw@amazon.co.uk>

Block devices don't work in PV Grub (0.9x) if there is no mode specified. It
complains: "Error ENOENT when reading the mode"

Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
---
 hw/block/xen-block.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/hw/block/xen-block.c b/hw/block/xen-block.c
index 034a18b70e..e61eab466c 100644
--- a/hw/block/xen-block.c
+++ b/hw/block/xen-block.c
@@ -408,6 +408,8 @@ static void xen_block_realize(XenDevice *xendev, Error **errp)
     }
 
     xen_device_backend_printf(xendev, "info", "%u", blockdev->info);
+    xen_device_backend_printf(xendev, "mode",
+                              (blockdev->info & VDISK_READONLY) ? "r" : "w");
 
     xen_device_frontend_printf(xendev, "virtual-device", "%lu",
                                vdev->number);
-- 
2.48.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] i386/xen: Move KVM_XEN_HVM_CONFIG ioctl to kvm_xen_init_vcpu()
  2025-02-07 14:37 [PATCH 1/2] i386/xen: Move KVM_XEN_HVM_CONFIG ioctl to kvm_xen_init_vcpu() David Woodhouse
  2025-02-07 14:37 ` [PATCH 2/2] hw/xen: Add "mode" parameter to xen-block devices David Woodhouse
@ 2025-02-07 15:37 ` Sean Christopherson
  2025-02-07 21:06   ` David Woodhouse
  2025-03-29  7:28 ` David Woodhouse
  2 siblings, 1 reply; 6+ messages in thread
From: Sean Christopherson @ 2025-02-07 15:37 UTC (permalink / raw)
  To: David Woodhouse
  Cc: qemu-devel, Stefano Stabellini, Anthony PERARD, Paul Durrant,
	Edgar E. Iglesias, Kevin Wolf, Hanna Reitz, Paolo Bonzini,
	Marcelo Tosatti, xen-devel, qemu-block, kvm

On Fri, Feb 07, 2025, David Woodhouse wrote:
> From: David Woodhouse <dwmw@amazon.co.uk>
> 
> 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.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] i386/xen: Move KVM_XEN_HVM_CONFIG ioctl to kvm_xen_init_vcpu()
  2025-02-07 15:37 ` [PATCH 1/2] i386/xen: Move KVM_XEN_HVM_CONFIG ioctl to kvm_xen_init_vcpu() Sean Christopherson
@ 2025-02-07 21:06   ` David Woodhouse
  0 siblings, 0 replies; 6+ messages in thread
From: David Woodhouse @ 2025-02-07 21:06 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: qemu-devel, Stefano Stabellini, Anthony PERARD, Paul Durrant,
	Edgar E. Iglesias, Kevin Wolf, Hanna Reitz, Paolo Bonzini,
	Marcelo Tosatti, xen-devel, qemu-block, kvm

On 7 February 2025 15:37:40 GMT, Sean Christopherson <seanjc@google.com> wrote:
>On Fri, Feb 07, 2025, David Woodhouse wrote:
>> From: David Woodhouse <dwmw@amazon.co.uk>
>> 
>> 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 :)


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] hw/xen: Add "mode" parameter to xen-block devices
  2025-02-07 14:37 ` [PATCH 2/2] hw/xen: Add "mode" parameter to xen-block devices David Woodhouse
@ 2025-03-07 10:38   ` Anthony PERARD
  0 siblings, 0 replies; 6+ messages in thread
From: Anthony PERARD @ 2025-03-07 10:38 UTC (permalink / raw)
  To: David Woodhouse
  Cc: qemu-devel, Stefano Stabellini, Paul Durrant, Edgar E. Iglesias,
	Kevin Wolf, Hanna Reitz, Paolo Bonzini, Marcelo Tosatti,
	xen-devel, qemu-block, kvm, Sean Christopherson

On Fri, Feb 07, 2025 at 02:37:24PM +0000, David Woodhouse wrote:
> From: David Woodhouse <dwmw@amazon.co.uk>
> 
> Block devices don't work in PV Grub (0.9x) if there is no mode specified. It
> complains: "Error ENOENT when reading the mode"
> 
> Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>

Reviewed-by: Anthony PERARD <anthony.perard@vates.tech>

Thanks,

-- 
Anthony PERARD


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] i386/xen: Move KVM_XEN_HVM_CONFIG ioctl to kvm_xen_init_vcpu()
  2025-02-07 14:37 [PATCH 1/2] i386/xen: Move KVM_XEN_HVM_CONFIG ioctl to kvm_xen_init_vcpu() David Woodhouse
  2025-02-07 14:37 ` [PATCH 2/2] hw/xen: Add "mode" parameter to xen-block devices David Woodhouse
  2025-02-07 15:37 ` [PATCH 1/2] i386/xen: Move KVM_XEN_HVM_CONFIG ioctl to kvm_xen_init_vcpu() Sean Christopherson
@ 2025-03-29  7:28 ` David Woodhouse
  2 siblings, 0 replies; 6+ messages in thread
From: David Woodhouse @ 2025-03-29  7:28 UTC (permalink / raw)
  To: qemu-devel
  Cc: Stefano Stabellini, Anthony PERARD, Paul Durrant,
	Edgar E. Iglesias, Kevin Wolf, Hanna Reitz, Paolo Bonzini,
	Marcelo Tosatti, xen-devel, qemu-block, kvm, Sean Christopherson

[-- Attachment #1: Type: text/plain, Size: 509 bytes --]

On Fri, 2025-02-07 at 14:37 +0000, David Woodhouse wrote:
> From: David Woodhouse <dwmw@amazon.co.uk>
> 
> 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 <dwmw@amazon.co.uk>

Ping?

[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5069 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-03-29  7:28 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-07 14:37 [PATCH 1/2] i386/xen: Move KVM_XEN_HVM_CONFIG ioctl to kvm_xen_init_vcpu() David Woodhouse
2025-02-07 14:37 ` [PATCH 2/2] hw/xen: Add "mode" parameter to xen-block devices David Woodhouse
2025-03-07 10:38   ` Anthony PERARD
2025-02-07 15:37 ` [PATCH 1/2] i386/xen: Move KVM_XEN_HVM_CONFIG ioctl to kvm_xen_init_vcpu() Sean Christopherson
2025-02-07 21:06   ` David Woodhouse
2025-03-29  7:28 ` David Woodhouse

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 NNTP newsgroup(s).