diff mbox series

[1/2] hw/xen: detect when running inside stubdomain

Message ID 20240219181627.282097-1-marmarek@invisiblethingslab.com (mailing list archive)
State Superseded
Headers show
Series [1/2] hw/xen: detect when running inside stubdomain | expand

Commit Message

Marek Marczykowski-Górecki Feb. 19, 2024, 6:16 p.m. UTC
Introduce global xen_is_stubdomain variable when qemu is running inside
a stubdomain instead of dom0. This will be relevant for subsequent
patches, as few things like accessing PCI config space need to be done
differently.

Signed-off-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
---
 hw/xen/xen-legacy-backend.c | 15 +++++++++++++++
 include/hw/xen/xen.h        |  1 +
 system/globals.c            |  1 +
 3 files changed, 17 insertions(+)

Comments

Philippe Mathieu-Daudé Feb. 20, 2024, 6:50 a.m. UTC | #1
On 19/2/24 19:16, Marek Marczykowski-Górecki wrote:
> Introduce global xen_is_stubdomain variable when qemu is running inside
> a stubdomain instead of dom0. This will be relevant for subsequent
> patches, as few things like accessing PCI config space need to be done
> differently.
> 
> Signed-off-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
> ---
>   hw/xen/xen-legacy-backend.c | 15 +++++++++++++++
>   include/hw/xen/xen.h        |  1 +
>   system/globals.c            |  1 +
>   3 files changed, 17 insertions(+)


> diff --git a/include/hw/xen/xen.h b/include/hw/xen/xen.h
> index 37ecc91fc3..ecb89ecfc1 100644
> --- a/include/hw/xen/xen.h
> +++ b/include/hw/xen/xen.h
> @@ -36,6 +36,7 @@ enum xen_mode {
>   extern uint32_t xen_domid;
>   extern enum xen_mode xen_mode;
>   extern bool xen_domid_restrict;
> +extern bool xen_is_stubdomain;
>   
>   int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num);
>   int xen_set_pci_link_route(uint8_t link, uint8_t irq);
> diff --git a/system/globals.c b/system/globals.c
> index b6d4e72530..ac27d88bd4 100644
> --- a/system/globals.c
> +++ b/system/globals.c
> @@ -62,6 +62,7 @@ bool qemu_uuid_set;
>   uint32_t xen_domid;
>   enum xen_mode xen_mode = XEN_DISABLED;
>   bool xen_domid_restrict;
> +bool xen_is_stubdomain;

Note for myself, Paolo and Claudio, IIUC these fields belong
to TYPE_XEN_ACCEL in accel/xen/xen-all.c. Maybe resulting in
smth like:

-- >8 --
diff --git a/accel/xen/xen-all.c b/accel/xen/xen-all.c
index 5ff0cb8bd9..fc25d8c912 100644
--- a/accel/xen/xen-all.c
+++ b/accel/xen/xen-all.c
@@ -24,11 +24,31 @@
  #include "migration/global_state.h"
  #include "hw/boards.h"

-bool xen_allowed;
+struct XenAccelState
+{
+    AccelState parent_obj;
+
+    bool xen_allowed;
+
+    enum xen_mode xen_mode;
+
+    uint32_t xen_domid;
+    bool xen_domid_restrict;

  xc_interface *xen_xc;
  xenforeignmemory_handle *xen_fmem;
  xendevicemodel_handle *xen_dmod;
+};
+
+struct XenAccelOpsClass
+{
+    AccelOpsClass parent_class;
+
+    struct evtchn_backend_ops *xen_evtchn_ops;
+    struct gnttab_backend_ops *xen_gnttab_ops;
+    struct foreignmem_backend_ops *xen_foreignmem_ops;
+    struct xenstore_backend_ops *xen_xenstore_ops;
+}

  static void xenstore_record_dm_state(const char *state)
  {
@@ -114,6 +134,13 @@ static int xen_init(MachineState *ms)
      return 0;
  }

+static void xen_accel_init(Object *obj)
+{
+    XenAccelState *s = XEN_ACCEL(obj);
+
+    s->xen_mode = XEN_DISABLED;
+}
+
  static void xen_accel_class_init(ObjectClass *oc, void *data)
  {
      AccelClass *ac = ACCEL_CLASS(oc);
@@ -142,6 +169,8 @@ static void xen_accel_class_init(ObjectClass *oc, 
void *data)
  static const TypeInfo xen_accel_type = {
      .name = TYPE_XEN_ACCEL,
      .parent = TYPE_ACCEL,
+    .instance_size = sizeof(XenAccelState),
+    .instance_init = xen_accel_init,
      .class_init = xen_accel_class_init,
  };

@@ -157,6 +186,7 @@ static const TypeInfo xen_accel_ops_type = {

      .parent = TYPE_ACCEL_OPS,
      .class_init = xen_accel_ops_class_init,
+    .class_size = sizeof(XenAccelOpsClass),
      .abstract = true,
  };

---
Jason Andryuk Feb. 25, 2024, 10:05 p.m. UTC | #2
On Mon, Feb 19, 2024 at 1:17 PM Marek Marczykowski-Górecki
<marmarek@invisiblethingslab.com> wrote:
>
> Introduce global xen_is_stubdomain variable when qemu is running inside
> a stubdomain instead of dom0. This will be relevant for subsequent
> patches, as few things like accessing PCI config space need to be done
> differently.
>
> Signed-off-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>

Reviewed-by: Jason Andryuk <jandryuk@gmail.com>

Thanks,
Jason
Jason Andryuk Feb. 25, 2024, 10:09 p.m. UTC | #3
On Tue, Feb 20, 2024 at 1:50 AM Philippe Mathieu-Daudé
<philmd@linaro.org> wrote:
>
> On 19/2/24 19:16, Marek Marczykowski-Górecki wrote:
> > Introduce global xen_is_stubdomain variable when qemu is running inside
> > a stubdomain instead of dom0. This will be relevant for subsequent
> > patches, as few things like accessing PCI config space need to be done
> > differently.
> >
> > Signed-off-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
> > ---
> >   hw/xen/xen-legacy-backend.c | 15 +++++++++++++++
> >   include/hw/xen/xen.h        |  1 +
> >   system/globals.c            |  1 +
> >   3 files changed, 17 insertions(+)
>
>
> > diff --git a/include/hw/xen/xen.h b/include/hw/xen/xen.h
> > index 37ecc91fc3..ecb89ecfc1 100644
> > --- a/include/hw/xen/xen.h
> > +++ b/include/hw/xen/xen.h
> > @@ -36,6 +36,7 @@ enum xen_mode {
> >   extern uint32_t xen_domid;
> >   extern enum xen_mode xen_mode;
> >   extern bool xen_domid_restrict;
> > +extern bool xen_is_stubdomain;
> >
> >   int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num);
> >   int xen_set_pci_link_route(uint8_t link, uint8_t irq);
> > diff --git a/system/globals.c b/system/globals.c
> > index b6d4e72530..ac27d88bd4 100644
> > --- a/system/globals.c
> > +++ b/system/globals.c
> > @@ -62,6 +62,7 @@ bool qemu_uuid_set;
> >   uint32_t xen_domid;
> >   enum xen_mode xen_mode = XEN_DISABLED;
> >   bool xen_domid_restrict;
> > +bool xen_is_stubdomain;
>
> Note for myself, Paolo and Claudio, IIUC these fields belong
> to TYPE_XEN_ACCEL in accel/xen/xen-all.c. Maybe resulting in
> smth like:

I think some of these are used by the KVM Xen-emulation, so they can't
just be moved into the Xen accelerator.  David?

Regards,
Jason
Philippe Mathieu-Daudé Feb. 26, 2024, 8:23 a.m. UTC | #4
On 19/2/24 19:16, Marek Marczykowski-Górecki wrote:
> Introduce global xen_is_stubdomain variable when qemu is running inside
> a stubdomain instead of dom0. This will be relevant for subsequent
> patches, as few things like accessing PCI config space need to be done
> differently.
> 
> Signed-off-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
> ---
>   hw/xen/xen-legacy-backend.c | 15 +++++++++++++++
>   include/hw/xen/xen.h        |  1 +
>   system/globals.c            |  1 +
>   3 files changed, 17 insertions(+)


> +static bool xen_check_stubdomain(void)
> +{
> +    char *dm_path = g_strdup_printf("/local/domain/%d/image", xen_domid);
> +    uint32_t dm_domid;
> +    bool is_stubdom = false;
> +
> +    if (!xenstore_read_int(dm_path, "device-model-domid", &dm_domid))

BTW missing braces for QEMU coding style: {

> +        is_stubdom = dm_domid != 0;

}

> +
> +    g_free(dm_path);
> +    return is_stubdom;
> +}
diff mbox series

Patch

diff --git a/hw/xen/xen-legacy-backend.c b/hw/xen/xen-legacy-backend.c
index 124dd5f3d6..4a09ea2561 100644
--- a/hw/xen/xen-legacy-backend.c
+++ b/hw/xen/xen-legacy-backend.c
@@ -603,6 +603,19 @@  static void xen_set_dynamic_sysbus(void)
     machine_class_allow_dynamic_sysbus_dev(mc, TYPE_XENSYSDEV);
 }
 
+static bool xen_check_stubdomain(void)
+{
+    char *dm_path = g_strdup_printf("/local/domain/%d/image", xen_domid);
+    uint32_t dm_domid;
+    bool is_stubdom = false;
+
+    if (!xenstore_read_int(dm_path, "device-model-domid", &dm_domid))
+        is_stubdom = dm_domid != 0;
+
+    g_free(dm_path);
+    return is_stubdom;
+}
+
 void xen_be_init(void)
 {
     xenstore = qemu_xen_xs_open();
@@ -616,6 +629,8 @@  void xen_be_init(void)
         exit(1);
     }
 
+    xen_is_stubdomain = xen_check_stubdomain();
+
     xen_sysdev = qdev_new(TYPE_XENSYSDEV);
     sysbus_realize_and_unref(SYS_BUS_DEVICE(xen_sysdev), &error_fatal);
     xen_sysbus = qbus_new(TYPE_XENSYSBUS, xen_sysdev, "xen-sysbus");
diff --git a/include/hw/xen/xen.h b/include/hw/xen/xen.h
index 37ecc91fc3..ecb89ecfc1 100644
--- a/include/hw/xen/xen.h
+++ b/include/hw/xen/xen.h
@@ -36,6 +36,7 @@  enum xen_mode {
 extern uint32_t xen_domid;
 extern enum xen_mode xen_mode;
 extern bool xen_domid_restrict;
+extern bool xen_is_stubdomain;
 
 int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num);
 int xen_set_pci_link_route(uint8_t link, uint8_t irq);
diff --git a/system/globals.c b/system/globals.c
index b6d4e72530..ac27d88bd4 100644
--- a/system/globals.c
+++ b/system/globals.c
@@ -62,6 +62,7 @@  bool qemu_uuid_set;
 uint32_t xen_domid;
 enum xen_mode xen_mode = XEN_DISABLED;
 bool xen_domid_restrict;
+bool xen_is_stubdomain;
 struct evtchn_backend_ops *xen_evtchn_ops;
 struct gnttab_backend_ops *xen_gnttab_ops;
 struct foreignmem_backend_ops *xen_foreignmem_ops;