Skip to contentSkip to conversation

xenstored patches from split hardware control

PATCH Superseded Jason Andryuk 2 participants 24 messages Jul 25, 2025

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover xenstored patches from split hardware control

This is a subset of patches focusing on xenstored changes from my split hardware control domain series.

It should address the stubdom breakage from the previous series. Due to a lack of x86 test systems, I haven't tested with a stubdom yet :( I'll try to remedy that.

"tools/xenstored: Use priv_domid for manual nodes and permission" is an interesting result of looking to rename some internal variables to better align their purpose.

Any review or guidance on the approach is appreciated.

Jason Andryuk (12):
xen: Add capabilities to get_domain_state
tools/manage: Expose domain capabilities
public/io: xs_wire: Include event channel in interface page xen/dom0less: store xenstore event channel in page tools/xenstored: Read event channel from xenstored page tools/xenstored: Add get_domain_evtchn() to find evtchn tools/xenstored: Auto-introduce domains tools/xenstored: Remove stubdom special casing tools/xenstored: Use priv_domid for manual nodes and permission tools/xenstored: Rename dom0_domid to store_domid tools/xenstored: Rename xenbus_master_domid -> xenbus_store_domain tools/xenstored: Remove hardcoded implicit path

 tools/include/xenmanage.h               |  14 ++-
 tools/libs/manage/core.c                |  21 +++-
 tools/xenstored/core.c                  |  15 ++-
 tools/xenstored/core.h                  |   6 +-
 tools/xenstored/domain.c                | 146 ++++++++++++++++++------
 tools/xenstored/domain.h                |   2 +-
 tools/xenstored/minios.c                |   4 +-
 tools/xenstored/posix.c                 |   2 +-
 xen/common/device-tree/dom0less-build.c |   7 ++
 xen/common/domain.c                     |  10 +-
 xen/include/public/domctl.h             |   7 +-
 xen/include/public/io/xs_wire.h         |   7 ++
 12 files changed, 182 insertions(+), 59 deletions(-)
Signature · 1 line
2.50.1
#1 lore raw

PATCH v4 1/12 xen: Add capabilities to get_domain_state

Expose a domain's capabilities - control, hardware or xenstore - through stable get domain state hypercall.

The xenstore domain can use this information to assign appropriate permissions on connections.

Repurpose the 16bit pad field for this purpose.

---
v3:
Add Stefano's R-b

v2:
Init info->caps = 0
Remove stale comment on caps field
Add Juergen's R-b

---
 xen/common/domain.c         | 10 +++++++++-
 xen/include/public/domctl.h |  7 +++++--
 2 files changed, 14 insertions(+), 3 deletions(-)
xen/common/domain.c +9 -1
index 303c338ef2..3c65cca5b0 100644
@@ -195,6 +195,14 @@ static void set_domain_state_info(struct xen_domctl_get_domain_state *info,
         info->state |= XEN_DOMCTL_GETDOMSTATE_STATE_DYING;
     if ( d->is_dying == DOMDYING_dead )
         info->state |= XEN_DOMCTL_GETDOMSTATE_STATE_DEAD;
+
+    info->caps = 0;
+    if ( is_control_domain(d) )
+        info->caps |= XEN_DOMCTL_GETDOMSTATE_CAP_CONTROL;
+    if ( is_hardware_domain(d) )
+        info->caps |= XEN_DOMCTL_GETDOMSTATE_CAP_HARDWARE;
+    if ( is_xenstore_domain(d) )
+        info->caps |= XEN_DOMCTL_GETDOMSTATE_CAP_XENSTORE;
     info->unique_id = d->unique_id;
 }
 
@@ -205,7 +213,7 @@ int get_domain_state(struct xen_domctl_get_domain_state *info, struct domain *d,
     int rc = -ENOENT;
     struct domain *hdl;
 
-    if ( info->pad0 || info->pad1 )
+    if ( info->pad0 )
         return -EINVAL;
 
     if ( d )
xen/include/public/domctl.h +5 -2
index a69dd96084..0c75d9d27f 100644
@@ -1265,8 +1265,11 @@ struct xen_domctl_get_domain_state {
 #define XEN_DOMCTL_GETDOMSTATE_STATE_SHUTDOWN  0x0002  /* Shutdown finished. */
 #define XEN_DOMCTL_GETDOMSTATE_STATE_DYING     0x0004  /* Domain dying. */
 #define XEN_DOMCTL_GETDOMSTATE_STATE_DEAD      0x0008  /* Domain dead. */
-    uint16_t pad0;           /* Must be 0 on input, returned as 0. */
-    uint32_t pad1;           /* Must be 0 on input, returned as 0. */
+    uint16_t caps;
+#define XEN_DOMCTL_GETDOMSTATE_CAP_CONTROL     0x0001  /* Control domain. */
+#define XEN_DOMCTL_GETDOMSTATE_CAP_HARDWARE    0x0002  /* Hardware domain. */
+#define XEN_DOMCTL_GETDOMSTATE_CAP_XENSTORE    0x0004  /* Xenstore domain. */
+    uint32_t pad0;           /* Must be 0 on input, returned as 0. */
     uint64_t unique_id;      /* Unique domain identifier. */
 };
 
Signature · 1 line
2.50.1
#2 lore raw

PATCH v4 2/12 tools/manage: Expose domain capabilities

In reply to Jason Andryuk

Add an additional "caps" argument to the libxenmanage functions to obtain a domains capabilities - control, hardware, and xenstore.

Update the xenstored callers at the same time.

---
v2:
Add Juergen's R-b

---
 tools/include/xenmanage.h | 14 ++++++++++++--
 tools/libs/manage/core.c  | 21 +++++++++++++++++----
 tools/xenstored/domain.c  |  8 ++++----
 3 files changed, 33 insertions(+), 10 deletions(-)
tools/include/xenmanage.h +12 -2
index 956b7a0a44..6fc0d9fe24 100644
@@ -45,6 +45,12 @@ int xenmanage_close(xenmanage_handle *hdl);
 #define XENMANAGE_GETDOMSTATE_STATE_DYING     0x0004  /* Domain dying. */
 #define XENMANAGE_GETDOMSTATE_STATE_DEAD      0x0008  /* Domain dead. */
 
+/* Control Domain capability. */
+#define XENMANAGE_GETDOMSTATE_CAP_CONTROL     0x0001
+/* Hardware Domain capability. */
+#define XENMANAGE_GETDOMSTATE_CAP_HARDWARE    0x0002
+/* Xenstore Domain capability. */
+#define XENMANAGE_GETDOMSTATE_CAP_XENSTORE    0x0004
 /*
  * Return state information of an existing domain.
  *
@@ -59,7 +65,8 @@ int xenmanage_close(xenmanage_handle *hdl);
  * Return value: 0 if information was stored, -1 else (errno is set)
  */
 int xenmanage_get_domain_info(xenmanage_handle *hdl, unsigned int domid,
-                              unsigned int *state, uint64_t *unique_id);
+                              unsigned int *state, unsigned int *caps,
+                              uint64_t *unique_id);
 
 /*
  * Return information of a domain having changed state recently.
@@ -73,12 +80,15 @@ int xenmanage_get_domain_info(xenmanage_handle *hdl, unsigned int domid,
  *   domid:     where to store the domid of the domain (not NULL)
  *   state:     where to store the state (XENMANAGE_GETDOMSTATE_STATE_ flags,
  *              nothing stored if NULL)
+ *   caps:      where to store the capabilities (XENMANAGE_GETDOMSTATE_CAP_
+ *              flags, nothing stored if NULL)
  *   unique_id: where to store the unique id of the domain (nothing stored if
  *              NULL)
  * Return value: 0 if information was stored, -1 else (errno is set)
  */
 int xenmanage_poll_changed_domain(xenmanage_handle *hdl, unsigned int *domid,
-                                  unsigned int *state, uint64_t *unique_id);
+                                  unsigned int *state, unsigned int *caps,
+                                  uint64_t *unique_id);
 #endif /* XENMANAGE_H */
 
 /*
tools/libs/manage/core.c +17 -4
index 8fb421df41..2fabdecaab 100644
@@ -92,6 +92,7 @@ static int xenmanage_do_domctl_get_domain_state(xenmanage_handle *hdl,
                                                 unsigned int domid_in,
                                                 unsigned int *domid_out,
                                                 unsigned int *state,
+                                                unsigned int *caps,
                                                 uint64_t *unique_id)
 {
     struct xen_domctl *buf;
@@ -130,6 +131,16 @@ static int xenmanage_do_domctl_get_domain_state(xenmanage_handle *hdl,
             if ( st->state & XEN_DOMCTL_GETDOMSTATE_STATE_DEAD )
                 *state |= XENMANAGE_GETDOMSTATE_STATE_DEAD;
         }
+        if ( caps )
+        {
+            *caps = 0;
+            if ( st->caps & XEN_DOMCTL_GETDOMSTATE_CAP_CONTROL )
+                *caps |= XENMANAGE_GETDOMSTATE_CAP_CONTROL;
+            if ( st->caps & XEN_DOMCTL_GETDOMSTATE_CAP_HARDWARE )
+                *caps |= XENMANAGE_GETDOMSTATE_CAP_HARDWARE;
+            if ( st->caps & XEN_DOMCTL_GETDOMSTATE_CAP_XENSTORE )
+                *caps |= XENMANAGE_GETDOMSTATE_CAP_XENSTORE;
+        }
         if ( unique_id )
             *unique_id = st->unique_id;
     }
@@ -142,7 +153,8 @@ static int xenmanage_do_domctl_get_domain_state(xenmanage_handle *hdl,
 }
 
 int xenmanage_get_domain_info(xenmanage_handle *hdl, unsigned int domid,
-                              unsigned int *state, uint64_t *unique_id)
+                              unsigned int *state, unsigned int *caps,
+                              uint64_t *unique_id)
 {
     if ( !hdl || domid >= DOMID_FIRST_RESERVED )
     {
@@ -150,12 +162,13 @@ int xenmanage_get_domain_info(xenmanage_handle *hdl, unsigned int domid,
         return -1;
     }
 
-    return xenmanage_do_domctl_get_domain_state(hdl, domid, NULL, state,
+    return xenmanage_do_domctl_get_domain_state(hdl, domid, NULL, state, caps,
                                                 unique_id);
 }
 
 int xenmanage_poll_changed_domain(xenmanage_handle *hdl, unsigned int *domid,
-                                  unsigned int *state, uint64_t *unique_id)
+                                  unsigned int *state, unsigned int *caps,
+                                  uint64_t *unique_id)
 {
     if ( !hdl || !domid )
     {
@@ -164,5 +177,5 @@ int xenmanage_poll_changed_domain(xenmanage_handle *hdl, unsigned int *domid,
     }
 
     return xenmanage_do_domctl_get_domain_state(hdl, DOMID_INVALID, domid,
-                                                state, unique_id);
+                                                state, caps, unique_id);
 }
tools/xenstored/domain.c +4 -4
index e1d5e8d614..f119d714ac 100644
@@ -667,7 +667,7 @@ static int check_domain(const void *k, void *v, void *arg)
 	unsigned int state;
 	uint64_t unique_id;
 
-	if (xenmanage_get_domain_info(xm_handle, domain->domid, &state,
+	if (xenmanage_get_domain_info(xm_handle, domain->domid, &state, NULL,
 				      &unique_id)) {
 		unique_id = 0;
 		state = 0;
@@ -700,7 +700,7 @@ static void do_check_domains(void)
 	struct domain *domain;
 	bool notify = false;
 
-	while (!xenmanage_poll_changed_domain(xm_handle, &domid, &state,
+	while (!xenmanage_poll_changed_domain(xm_handle, &domid, &state, NULL,
 					      &unique_id)) {
 		domain = find_domain_struct(domid);
 		if (domain)
@@ -829,7 +829,7 @@ static struct domain *find_or_alloc_existing_domain(unsigned int domid)
 	domain = find_domain_struct(domid);
 	if (!domain || !domain->unique_id)
 		dom_valid = !xenmanage_get_domain_info(xm_handle, domid,
-						       NULL, &unique_id);
+						       NULL, NULL, &unique_id);
 
 	if (dom_valid) {
 		if (!domain)
@@ -1377,7 +1377,7 @@ int domain_alloc_permrefs(struct node_perms *perms)
 		domid = perms->p[i].id;
 		d = find_domain_struct(domid);
 		if (!d) {
-			if (xenmanage_get_domain_info(xm_handle, domid,
+			if (xenmanage_get_domain_info(xm_handle, domid, NULL,
 						      NULL, &unique_id))
 				perms->p[i].perms |= XS_PERM_IGNORE;
 			else if (!alloc_domain(NULL, domid, unique_id))
Signature · 1 line
2.50.1
#3 lore raw

PATCH v4 3/12 public/io: xs_wire: Include event channel in interface page

In reply to Jason Andryuk

Include the event channel in struct xenstore_domain_interface. This way the toolstack or xen can communicate the event channel to xenstored in memory xenstored already needs to access.

xenstored maps the grant with the well known GNTTAB_RESERVED_XENSTORE index, so no further information is needed.

---
v2:
s/domU/domain/ in comment
Add Juergen's R-b

---
 xen/include/public/io/xs_wire.h | 7 +++++++
 1 file changed, 7 insertions(+)
xen/include/public/io/xs_wire.h +7 -0
index e92a87a07b..d2e2b8b9eb 100644
@@ -110,6 +110,7 @@ struct xenstore_domain_interface {
     uint32_t server_features; /* Bitmap of features supported by the server */
     uint32_t connection;
     uint32_t error;
+    uint32_t evtchn_port;
 };
 
 /* Violating this is very bad.  See docs/misc/xenstore.txt. */
@@ -134,6 +135,12 @@ struct xenstore_domain_interface {
 #define XENSTORE_ERROR_RINGIDX 2 /* Invalid ring index */
 #define XENSTORE_ERROR_PROTO   3 /* Protocol violation (payload too long) */
 
+/*
+ * The evtchn_port field is the domain's event channel for xenstored to signal.
+ * It is filled in by Xen for dom0less/Hyperlaunch domains.  It is only used
+ * when non-zero.  Otherwise the event channel from XS_INTRODUCE is used.
+ */
+
 #endif /* _XS_WIRE_H */
 
 /*
Signature · 1 line
2.50.1
#4 lore raw

PATCH v4 4/12 xen/dom0less: store xenstore event channel in page

In reply to Jason Andryuk

Write the associated event channel into the xenstore page so xenstored can read it. xenstored can map the grant by the reserved grant table entry, and then read out the event channel and bind it. This eliminates the need for an additional mechanism to discover the event channel.

---
This is ARM-only for the time being. A common function to write to guest physical address will be needed.

---
 xen/common/device-tree/dom0less-build.c | 7 +++++++
 1 file changed, 7 insertions(+)
xen/common/device-tree/dom0less-build.c +7 -0
index 6bb038111d..a96089d9bb 100644
@@ -26,6 +26,7 @@
 #include <public/event_channel.h>
 #include <public/io/xs_wire.h>
 
+#include <asm/guest_access.h>
 #include <asm/setup.h>
 
 #include <xen/static-memory.h>
@@ -120,8 +121,14 @@ static void __init initialize_domU_xenstore(void)
 
         if ( gfn != XENSTORE_PFN_LATE_ALLOC && IS_ENABLED(CONFIG_GRANT_TABLE) )
         {
+            evtchn_port_t port = d->arch.hvm.params[HVM_PARAM_STORE_EVTCHN];
+            paddr_t evtchn_gaddr = gfn_to_gaddr(_gfn(gfn)) +
+                offsetof(struct xenstore_domain_interface, evtchn_port);
+
             ASSERT(gfn < UINT32_MAX);
             gnttab_seed_entry(d, GNTTAB_RESERVED_XENSTORE, xs_domid, gfn);
+            access_guest_memory_by_gpa(d, evtchn_gaddr, &port, sizeof(port),
+                                       true /* is_write */);
         }
     }
 }
Signature · 1 line
2.50.1
#5 lore raw

PATCH v4 5/12 tools/xenstored: Read event channel from xenstored page

In reply to Jason Andryuk

Make introduce_domain() use an event channel from the the xenstore page. It is only used if non-zero. Otherwise the passed in event channel port is used.

The is useful for a xenstored stubdom to configure domains autonomously.

---
v2:
Remove iface_port variable.

---
 tools/xenstored/domain.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/xenstored/domain.c +4 -0
index f119d714ac..1241f8c73e 100644
@@ -1024,6 +1024,10 @@ static struct domain *introduce_domain(const void *ctx,
 		interface = map_interface(domid);
 		if (!interface && !restore)
 			return NULL;
+
+		if (interface->evtchn_port)
+			port = interface->evtchn_port;
+
 		if (new_domain(domain, port, restore)) {
 			rc = errno;
 			if (interface)
Signature · 1 line
2.50.1
Reviewed-by on v4 5/12
#6 lore raw

Re: [PATCH v4 05/12] tools/xenstored: Read event channel from xenstored page

Quoted 7 lines · Jason Andryuk

Make introduce_domain() use an event channel from the the xenstore page. It is only used if non-zero. Otherwise the passed in event channel port is used.

The is useful for a xenstored stubdom to configure domains autonomously.

Juergen

#7 lore raw

PATCH v4 6/12 tools/xenstored: Add get_domain_evtchn() to find evtchn

In reply to Jason Andryuk

Add a helper to lookup the event channel for a domid. This hides some of the differences between dom0 and stubdom xenstored.

It highlights the different meanings between get_xenbus_evtchn() in a stubdom, where it looks up dom0's event channel, and dom0, where it looks up the local event channel.

The default return 0 will be fine as any other auto-introduced domain will needs the event channel populated in the grant.

---
 tools/xenstored/domain.c | 38 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)
tools/xenstored/domain.c +36 -2
index 1241f8c73e..1c52254ba8 100644
@@ -1251,12 +1251,41 @@ const char *get_implicit_path(const struct connection *conn)
 	return conn->domain->path;
 }
 
+/*
+ * dom0 xenstored (posix.c) uses get_xenbus_evtchn() to lookup with
+ * XENSTORED_PORT_DEV.
+ *
+ * minios stubdom uses get_xenbus_evtchn() to look up dom0's event channel
+ * from the command line (--event).  The stubdom's own event channel is
+ * returned directly.
+ *
+ * Any other existing domains from dom0less/Hyperlaunch will have
+ * the event channel in the xenstore page, so lookup here isn't necessary.
+ * --event would not be set, so it would default to 0.
+ */
+evtchn_port_t get_domain_evtchn(domid_t domid)
+{
+#ifdef __MINIOS__
+	if (domid == stub_domid) {
+		return xenbus_evtchn;
+	} else if (domid == priv_domid) {
+		return get_xenbus_evtchn();
+	}
+#else
+	if (domid == xenbus_master_domid()) {
+		return get_xenbus_evtchn();
+	}
+#endif
+
+	return 0;
+}
+
 void dom0_init(void)
 {
 	evtchn_port_t port;
 	struct domain *dom0;
 
-	port = get_xenbus_evtchn();
+	port = get_domain_evtchn(xenbus_master_domid());
 	if (port == -1)
 		barf_perror("Failed to initialize dom0 port");
 
@@ -1271,11 +1300,16 @@ void stubdom_init(void)
 {
 #ifdef __MINIOS__
 	struct domain *stubdom;
+	evtchn_port_t port;
 
 	if (stub_domid < 0)
 		return;
 
-	stubdom = introduce_domain(NULL, stub_domid, xenbus_evtchn, false);
+	port = get_domain_evtchn(stub_domid);
+	if (port == -1)
+		barf_perror("Failed to initialize dom0 port");
+
+	stubdom = introduce_domain(NULL, stub_domid, port, false);
 	if (!stubdom)
 		barf_perror("Failed to initialize stubdom");
 
Signature · 1 line
2.50.1
#8 lore raw

Re: [PATCH v4 06/12] tools/xenstored: Add get_domain_evtchn() to find evtchn

Quoted 42 lines · Jason Andryuk

Add a helper to lookup the event channel for a domid. This hides some of the differences between dom0 and stubdom xenstored.

It highlights the different meanings between get_xenbus_evtchn() in a stubdom, where it looks up dom0's event channel, and dom0, where it looks up the local event channel.

The default return 0 will be fine as any other auto-introduced domain will needs the event channel populated in the grant.

---
  tools/xenstored/domain.c | 38 ++++++++++++++++++++++++++++++++++++--
  1 file changed, 36 insertions(+), 2 deletions(-)
tools/xenstored/domain.c +19 -0
index 1241f8c73e..1c52254ba8 100644
@@ -1251,12 +1251,41 @@ const char *get_implicit_path(const struct connection *conn)
  	return conn->domain->path;
  }
 
+/*
+ * dom0 xenstored (posix.c) uses get_xenbus_evtchn() to lookup with
+ * XENSTORED_PORT_DEV.
+ *
+ * minios stubdom uses get_xenbus_evtchn() to look up dom0's event channel
+ * from the command line (--event).  The stubdom's own event channel is
+ * returned directly.
+ *
+ * Any other existing domains from dom0less/Hyperlaunch will have
+ * the event channel in the xenstore page, so lookup here isn't necessary.
+ * --event would not be set, so it would default to 0.
+ */
+evtchn_port_t get_domain_evtchn(domid_t domid)
+{
+#ifdef __MINIOS__
+	if (domid == stub_domid) {
+		return xenbus_evtchn;
+	} else if (domid == priv_domid) {
+		return get_xenbus_evtchn();

tools/xenstored/domain.c const char *get_implicit_path(const struct connection *conn) · line 1282

Quoted 10 lines
tools/xenstored/domain.c +10 -0
+	}
+#else
+	if (domid == xenbus_master_domid()) {
+		return get_xenbus_evtchn();
+	}
+#endif
+
+	return 0;
+}
+

I'd prefer to have 2 get_domain_evtchn() variants, one in posix.c and one in minios.c. This way you can avoid the #ifdef.

Quoted 13 lines
tools/xenstored/domain.c +1 -1
  void dom0_init(void)
  {
  	evtchn_port_t port;
  	struct domain *dom0;
 
-	port = get_xenbus_evtchn();
+	port = get_domain_evtchn(xenbus_master_domid());
  	if (port == -1)
  		barf_perror("Failed to initialize dom0 port");
 
@@ -1271,11 +1300,16 @@ void stubdom_init(void)
  {
  #ifdef __MINIOS__

tools/xenstored/domain.c void stubdom_init(void) · line 1310

Quoted 10 lines
tools/xenstored/domain.c +4 -1
  	struct domain *stubdom;
+	evtchn_port_t port;
 
  	if (stub_domid < 0)
  		return;
 
-	stubdom = introduce_domain(NULL, stub_domid, xenbus_evtchn, false);
+	port = get_domain_evtchn(stub_domid);
+	if (port == -1)
+		barf_perror("Failed to initialize dom0 port");

s/dom0/stubdom/

tools/xenstored/domain.c void stubdom_init(void) · line 1315

Quoted 5 lines
tools/xenstored/domain.c +2 -0
+
+	stubdom = introduce_domain(NULL, stub_domid, port, false);
  	if (!stubdom)
  		barf_perror("Failed to initialize stubdom");
 

Juergen

#9 lore raw

Re: [PATCH v4 06/12] tools/xenstored: Add get_domain_evtchn() to find evtchn

Quoted 50 lines · Jürgen Groß
Quoted 49 lines · Jason Andryuk

Add a helper to lookup the event channel for a domid.  This hides some of the differences between dom0 and stubdom xenstored.

It highlights the different meanings between get_xenbus_evtchn() in a stubdom, where it looks up dom0's event channel, and dom0, where it looks up the local event channel.

The default return 0 will be fine as any other auto-introduced domain will needs the event channel populated in the grant.

---
  tools/xenstored/domain.c | 38 ++++++++++++++++++++++++++++++++++++--

  1 file changed, 36 insertions(+), 2 deletions(-)
tools/xenstored/domain.c +5 -0
index 1241f8c73e..1c52254ba8 100644
@@ -1251,12 +1251,41 @@ const char *get_implicit_path(const struct 
 connection *conn)
       return conn->domain->path;
   }
+/*
+ * dom0 xenstored (posix.c) uses get_xenbus_evtchn() to lookup with
+ * XENSTORED_PORT_DEV.
+ *
+ * minios stubdom uses get_xenbus_evtchn() to look up dom0's event 

channel
+ * from the command line (--event).  The stubdom's own event channel is
+ * returned directly.
+ *
+ * Any other existing domains from dom0less/Hyperlaunch will have
+ * the event channel in the xenstore page, so lookup here isn't necessary.
+ * --event would not be set, so it would default to 0.
+ */

tools/xenstored/domain.c +12 -0
+evtchn_port_t get_domain_evtchn(domid_t domid)
+{
+#ifdef __MINIOS__
+    if (domid == stub_domid) {
+        return xenbus_evtchn;
+    } else if (domid == priv_domid) {
+        return get_xenbus_evtchn();
+    }
+#else
+    if (domid == xenbus_master_domid()) {
+        return get_xenbus_evtchn();
+    }
Quoted 8 lines

tools/xenstored/domain.c const char *get_implicit_path(const struct connection *conn) · line 1282

Quoted 5 lines
tools/xenstored/domain.c +5 -0
+#endif
+
+    return 0;
+}
+

I'd prefer to have 2 get_domain_evtchn() variants, one in posix.c and one in minios.c. This way you can avoid the #ifdef.

Sounds good.

Quoted 13 lines
Quoted 13 lines

  void dom0_init(void)
  {
      evtchn_port_t port;
      struct domain *dom0;
-    port = get_xenbus_evtchn();
+    port = get_domain_evtchn(xenbus_master_domid()); if (port == -1)
          barf_perror("Failed to initialize dom0 port");

tools/xenstored/domain.c +1 -0
@@ -1271,11 +1300,16 @@ void stubdom_init(void)
   {
   #ifdef __MINIOS__
       struct domain *stubdom;
+    evtchn_port_t port;
Quoted 8 lines

tools/xenstored/domain.c void stubdom_init(void) · line 1310

Quoted 6 lines
tools/xenstored/domain.c +3 -1
       if (stub_domid < 0)
           return;
-    stubdom = introduce_domain(NULL, stub_domid, xenbus_evtchn, false);
+    port = get_domain_evtchn(stub_domid);
+    if (port == -1)
+        barf_perror("Failed to initialize dom0 port");

s/dom0/stubdom/

Thanks,
Jason

#10 lore raw

PATCH v4 7/12 tools/xenstored: Auto-introduce domains

In reply to Jason Andryuk

Replace dom0_init() with init_domains() which uses libxenmanage to iterate through all existing domains, storing them in a list. The xenstore domain is introduced first, and then all the other domains are introduced. The xenstore domain needs to be introduced first to setup structures needed for firing watches.

dom0_domid is updated with the xenstore domain, since it really indicates the local domain.

priv_domid is set to the control domain. This makes it limited to a single domain.

These features let xenstore automatically connect any existing domains, which means it doesn't need to be done manually from init-dom0less.

For a legacy dom0, the result should be unchanged.

For a late xenstore stubdom it should also be the same, but priv_domid would be set automatically to control domain (which default to 0 normally).

Always signal the event channel for initial domains. This gets dom0 (a local xenstored domain) to connect.

Also always write XENSTORE_CONNECTED since we know we are connected at this point.

To support ARM dom0less domains with xen,enhanced = "no-xenstore" a failed introduce_domain() becomes non-fatal. Normally, HVM_PARAM_STORE_EVTHCN is used to identify .

---
I noticed domain_conn_reset() isn't called for the stubdom, so I added the ifdef to special case it. I haven't tested with a stubdom yet, and I wanted to be conservative. Ideally it would be dropped - the issue would be if the stubdom queues requests before xenstore is in service.

---
 tools/xenstored/core.c   |  2 +-
 tools/xenstored/domain.c | 83 ++++++++++++++++++++++++++++------------
 tools/xenstored/domain.h |  2 +-
 3 files changed, 61 insertions(+), 26 deletions(-)
tools/xenstored/core.c +1 -1
index 37e4dd5a5b..c2f8d20211 100644
@@ -2757,7 +2757,7 @@ int main(int argc, char *argv[])
 	/* Listen to hypervisor. */
 	if (!live_update) {
 		domain_init(-1);
-		dom0_init();
+		init_domains();
 	}
 
 	/* redirect to /dev/null now we're ready to accept connections */
tools/xenstored/domain.c +59 -24

100 lines not on this page · raw message

tools/xenstored/domain.h +1 -1
index 844ac11510..6a78f06935 100644
@@ -84,7 +84,7 @@ int do_reset_watches(const void *ctx, struct connection *conn,
 
 void domain_early_init(void);
 void domain_init(int evtfd);
-void dom0_init(void);
+void init_domains(void);
 void stubdom_init(void);
 void domain_deinit(void);
 void ignore_connection(struct connection *conn, unsigned int err);
Signature · 1 line
2.50.1
#11 lore raw

Re: [PATCH v4 07/12] tools/xenstored: Auto-introduce domains

Quoted 24 lines · Jason Andryuk

Replace dom0_init() with init_domains() which uses libxenmanage to iterate through all existing domains, storing them in a list. The xenstore domain is introduced first, and then all the other domains are introduced. The xenstore domain needs to be introduced first to setup structures needed for firing watches.

dom0_domid is updated with the xenstore domain, since it really indicates the local domain.

priv_domid is set to the control domain. This makes it limited to a single domain.

These features let xenstore automatically connect any existing domains, which means it doesn't need to be done manually from init-dom0less.

For a legacy dom0, the result should be unchanged.

For a late xenstore stubdom it should also be the same, but priv_domid would be set automatically to control domain (which default to 0 normally).

Always signal the event channel for initial domains. This gets dom0 (a local xenstored domain) to connect.

Quoted 6 lines

Also always write XENSTORE_CONNECTED since we know we are connected at this point.

To support ARM dom0less domains with xen,enhanced = "no-xenstore" a failed introduce_domain() becomes non-fatal. Normally, HVM_PARAM_STORE_EVTHCN is used to identify .

HVM_PARAM_STORE_EVTCHN

Quoted 41 lines

---
I noticed domain_conn_reset() isn't called for the stubdom, so I added the ifdef to special case it. I haven't tested with a stubdom yet, and I wanted to be conservative. Ideally it would be dropped - the issue would be if the stubdom queues requests before xenstore is in service.

---
  tools/xenstored/core.c   |  2 +-
  tools/xenstored/domain.c | 83 ++++++++++++++++++++++++++++------------
  tools/xenstored/domain.h |  2 +-
  3 files changed, 61 insertions(+), 26 deletions(-)
tools/xenstored/core.c +1 -1
index 37e4dd5a5b..c2f8d20211 100644
@@ -2757,7 +2757,7 @@ int main(int argc, char *argv[])
  	/* Listen to hypervisor. */
  	if (!live_update) {
  		domain_init(-1);
-		dom0_init();
+		init_domains();
  	}
 
  	/* redirect to /dev/null now we're ready to accept connections */
tools/xenstored/domain.c +3 -3
index 1c52254ba8..e9e45ed8e8 100644
@@ -1280,41 +1280,76 @@ evtchn_port_t get_domain_evtchn(domid_t domid)
  	return 0;
  }
 
-void dom0_init(void)
+void init_domains(void)
  {
-	evtchn_port_t port;
-	struct domain *dom0;
+	unsigned int *domids = NULL;
+	unsigned int nr_domids = 0;

tools/xenstored/domain.c evtchn_port_t get_domain_evtchn(domid_t domid) · line 1296

Quoted 10 lines
tools/xenstored/domain.c +10 -0
+	unsigned int domid;
+	unsigned int state;
+	unsigned int caps;
+	uint64_t unique_id;
+	int introduce_count = 0;
+
+	while (!xenmanage_poll_changed_domain(xm_handle, &domid, &state, &caps,
+					      &unique_id)) {
+		nr_domids++;
+		domids = realloc(domids, nr_domids * sizeof(*domids));

Please use the talloc framework for memory allocations.

Quoted 4 lines
tools/xenstored/domain.c +4 -0
+		if (!domids)
+			barf_perror("Failed to reallocate domids");
+
+		domids[nr_domids - 1] = domid;

tools/xenstored/domain.c evtchn_port_t get_domain_evtchn(domid_t domid) · line 1310

Quoted 10 lines
tools/xenstored/domain.c +10 -0
+
+		if (caps & XENMANAGE_GETDOMSTATE_CAP_XENSTORE) {
+			memmove(&domids[1], domids, (nr_domids - 1) * sizeof(*domids));
+			/*
+			 * Local domid must be first to setup structures for
+			 * firing the special watches.
+			 */
+			domids[0] = domid;
+			dom0_domid = domid;
+		}

You could move the loop body below in a helper function and call that for the Xenstore domain just here without adding the domid to domids[]. This would require to do the XENMANAGE_GETDOMSTATE_CAP_XENSTORE test after all other caps tests.

tools/xenstored/domain.c evtchn_port_t get_domain_evtchn(domid_t domid) · line 1314

Quoted 7 lines
tools/xenstored/domain.c +3 -3
-	port = get_domain_evtchn(xenbus_master_domid());
-	if (port == -1)
-		barf_perror("Failed to initialize dom0 port");
+		if (caps & XENMANAGE_GETDOMSTATE_CAP_CONTROL)
+			priv_domid = domid;
+	}

Is it okay to overwrite priv_domid in case multiple domains have XENMANAGE_GETDOMSTATE_CAP_CONTROL active? Same question regarding XENMANAGE_GETDOMSTATE_CAP_XENSTORE above.

Quoted 20 lines
tools/xenstored/domain.c +13 -5
-	dom0 = introduce_domain(NULL, xenbus_master_domid(), port, false);
-	if (!dom0)
-		barf_perror("Failed to initialize dom0");
+	for (unsigned int i = 0; i < nr_domids; i++) {
+		evtchn_port_t port;
+		struct domain *domain;
+		domid = domids[i];
 
-	xenevtchn_notify(xce_handle, dom0->port);
-}
+		port = get_domain_evtchn(domid);
+		if (port == -1)
+			barf_perror("Failed to initialize dom%u port", domid);
+
+		domain = introduce_domain(NULL, domid, port, false);
+		if (!domain) {
+			xprintf("Could not initialize dom%u", domid);
+			continue;
+		}

tools/xenstored/domain.c evtchn_port_t get_domain_evtchn(domid_t domid) · line 1335

Quoted 10 lines
tools/xenstored/domain.c +4 -4
+		introduce_count++;
 
-void stubdom_init(void)
-{
  #ifdef __MINIOS__
-	struct domain *stubdom;
-	evtchn_port_t port;
+		if (domid != stub_domid)
+#endif
+			domain_conn_reset(domain);

You definitely don't want to call domain_conn_reset() here.

It is NOT called for dom0 today and for normal domUs it is called before the domain gets started.

Quoted 34 lines
tools/xenstored/domain.c +12 -9
-	if (stub_domid < 0)
-		return;
+		if (domain->interface)
+			domain->interface->connection = XENSTORE_CONNECTED;
 
-	port = get_domain_evtchn(stub_domid);
-	if (port == -1)
-		barf_perror("Failed to initialize dom0 port");
+		/* Notify the domain that xenstore is available */
+		xenevtchn_notify(xce_handle, domain->port);
+	}
 
-	stubdom = introduce_domain(NULL, stub_domid, port, false);
-	if (!stubdom)
-		barf_perror("Failed to initialize stubdom");
+	free(domids);
 
-	xenevtchn_notify(xce_handle, stubdom->port);
+	if (introduce_count == 0)
+		barf("Did not initialize any domains");
+}
 
+void stubdom_init(void)
+{
+#ifdef __MINIOS__
  	mount_9pfs();
  #endif
  }
tools/xenstored/domain.h +0 -0
index 844ac11510..6a78f06935 100644

tools/xenstored/domain.h int do_reset_watches(const void *ctx, struct connection *conn, · line 90

Quoted 8 lines
tools/xenstored/domain.h +1 -1
@@ -84,7 +84,7 @@ int do_reset_watches(const void *ctx, struct connection *conn,
 
  void domain_early_init(void);
  void domain_init(int evtfd);
-void dom0_init(void);
+void init_domains(void);
  void stubdom_init(void);
  void domain_deinit(void);
  void ignore_connection(struct connection *conn, unsigned int err);

Juergen

#12 lore raw

Re: [PATCH v4 07/12] tools/xenstored: Auto-introduce domains

Quoted 25 lines · Jürgen Groß
Quoted 24 lines · Jason Andryuk

Replace dom0_init() with init_domains() which uses libxenmanage to iterate through all existing domains, storing them in a list.  The xenstore domain is introduced first, and then all the other domains are introduced.  The xenstore domain needs to be introduced first to setup structures needed for firing watches.

dom0_domid is updated with the xenstore domain, since it really indicates the local domain.

priv_domid is set to the control domain.  This makes it limited to a single domain.

These features let xenstore automatically connect any existing domains, which means it doesn't need to be done manually from init-dom0less.

For a legacy dom0, the result should be unchanged.

For a late xenstore stubdom it should also be the same, but priv_domid would be set automatically to control domain (which default to 0 normally).

Always signal the event channel for initial domains.  This gets dom0 (a local xenstored domain) to connect.

Quoted 8 lines
Quoted 6 lines

Also always write XENSTORE_CONNECTED since we know we are connected at this point.

To support ARM dom0less domains with xen,enhanced = "no-xenstore" a failed introduce_domain() becomes non-fatal.  Normally, HVM_PARAM_STORE_EVTHCN is used to identify .

HVM_PARAM_STORE_EVTCHN

Thanks.

Quoted 27 lines
Quoted 27 lines

---
I noticed domain_conn_reset() isn't called for the stubdom, so I added the ifdef to special case it.  I haven't tested with a stubdom yet, and I wanted to be conservative.  Ideally it would be dropped - the issue would be if the stubdom queues requests before xenstore is in service.

tools/xenstored/domain.c +8 -3
index 1c52254ba8..e9e45ed8e8 100644
@@ -1280,41 +1280,76 @@ evtchn_port_t get_domain_evtchn(domid_t domid)
       return 0;
   }
-void dom0_init(void)
+void init_domains(void)
   {
-    evtchn_port_t port;
-    struct domain *dom0;
+    unsigned int *domids = NULL;
+    unsigned int nr_domids = 0;
+    unsigned int domid;
+    unsigned int state;
+    unsigned int caps;
+    uint64_t unique_id;
+    int introduce_count = 0;
Quoted 8 lines

tools/xenstored/domain.c evtchn_port_t get_domain_evtchn(domid_t domid) · line 1296

Quoted 6 lines
tools/xenstored/domain.c +5 -0
+
+    while (!xenmanage_poll_changed_domain(xm_handle, &domid, &state, 
 &caps,
+                          &unique_id)) {
+        nr_domids++;
+        domids = realloc(domids, nr_domids * sizeof(*domids));

Please use the talloc framework for memory allocations.

Yes, of course.

Quoted 12 lines
Quoted 12 lines
tools/xenstored/domain.c +11 -0
+        if (!domids)
+            barf_perror("Failed to reallocate domids");
+
+        domids[nr_domids - 1] = domid;
+
+        if (caps & XENMANAGE_GETDOMSTATE_CAP_XENSTORE) {
+            memmove(&domids[1], domids, (nr_domids - 1) * 
 sizeof(*domids));
+            /*
+             * Local domid must be first to setup structures for
+             * firing the special watches.
+             */
Quoted 8 lines

tools/xenstored/domain.c evtchn_port_t get_domain_evtchn(domid_t domid) · line 1310

Quoted 3 lines
tools/xenstored/domain.c +3 -0
+            domids[0] = domid;
+            dom0_domid = domid;
+        }

You could move the loop body below in a helper function and call that for the Xenstore domain just here without adding the domid to domids[]. This would require to do the XENMANAGE_GETDOMSTATE_CAP_XENSTORE test after all other caps tests.

Yes, good idea. Actually, given the "last set" xenstore domid issue I mention below, I'll move it between the two loops.

Quoted 9 lines

tools/xenstored/domain.c evtchn_port_t get_domain_evtchn(domid_t domid) · line 1314

Quoted 6 lines
tools/xenstored/domain.c +3 -3
-    port = get_domain_evtchn(xenbus_master_domid());
-    if (port == -1)
-        barf_perror("Failed to initialize dom0 port");
+        if (caps & XENMANAGE_GETDOMSTATE_CAP_CONTROL)
+            priv_domid = domid;
+    }

Is it okay to overwrite priv_domid in case multiple domains have XENMANAGE_GETDOMSTATE_CAP_CONTROL active?

Probably not, no. First domain, being domain 0, getting priv_domid is probably what we want by default. And we should respect the command line value if set.

if (priv_domid != DOMID_INVALID)

    priv_domid = domid;
Quoted 2 lines

Same question regarding
XENMANAGE_GETDOMSTATE_CAP_XENSTORE above.

dom0less only allows creating 1 xenstore domain... but the "late" stubdom case is different. dom0 and the stubdom will both have XENMANAGE_GETDOMSTATE_CAP_XENSTORE. Since in that case, we only have dom0 and dom1, letting the second one take does what we want. dom0 assigning VIRQ_DOM_EXC to another domain could transfer the capability, and then there would only be one. Similarly, passing --master-domid to minios would make it explicit. (I'm not a fan of passing the domain's own domid).

A more explicit way to convey the information would be better. Directly exposing the domid and the capabilities to the domain would let xenstored know exactly what it should configure.

Until then, the cases we have are either a single xenstore domain, or dom0 and dom1. Selecting the last one will do the right thing.

Quoted 30 lines
Quoted 17 lines
tools/xenstored/domain.c +12 -5
-    dom0 = introduce_domain(NULL, xenbus_master_domid(), port, false);
-    if (!dom0)
-        barf_perror("Failed to initialize dom0");
+    for (unsigned int i = 0; i < nr_domids; i++) {
+        evtchn_port_t port;
+        struct domain *domain;
+        domid = domids[i];
-    xenevtchn_notify(xce_handle, dom0->port);
-}
+        port = get_domain_evtchn(domid);
+        if (port == -1)
+            barf_perror("Failed to initialize dom%u port", domid);
+
+        domain = introduce_domain(NULL, domid, port, false);
+        if (!domain) {
+            xprintf("Could not initialize dom%u", domid);
+            continue;

tools/xenstored/domain.c evtchn_port_t get_domain_evtchn(domid_t domid) · line 1335

Quoted 10 lines
tools/xenstored/domain.c +5 -4
+        }
+        introduce_count++;
-void stubdom_init(void)
-{
   #ifdef __MINIOS__
-    struct domain *stubdom;
-    evtchn_port_t port;
+        if (domid != stub_domid)
+#endif
+            domain_conn_reset(domain);

You definitely don't want to call domain_conn_reset() here.

Quoted 2 lines

It is NOT called for dom0 today and for normal domUs it is called before the domain gets started.

Ok, thanks. I didn't look back far enough, so I didn't realize I added it myself. I did that to make this loop as close as possible to do_introduce() for the domUs. Since there shouldn't be anything to reset, I'll drop it.

Thanks,
Jason

#13 lore raw

PATCH v4 8/12 tools/xenstored: Remove stubdom special casing

In reply to Jason Andryuk

posix.c and minios.c implement the same named functions serving slightly different purposes.

For xenbus_map()
posix.c maps the local /dev/xen/xsd_kva
minios.c maps dom0 via grant and there is open coding for stub_domid in map_interface.

Change xenbus_map() to map the local domain's interface. The default grant table mapping is performed otherwise.

---
 tools/xenstored/domain.c | 5 -----
 tools/xenstored/minios.c | 4 +---
 2 files changed, 1 insertion(+), 8 deletions(-)
tools/xenstored/domain.c +0 -5
index e9e45ed8e8..ac4ac72f99 100644
@@ -506,11 +506,6 @@ static void *map_interface(domid_t domid)
 	if (domid == xenbus_master_domid())
 		return xenbus_map();
 
-#ifdef __MINIOS__
-	if (domid == stub_domid)
-		return xenstore_buf;
-#endif
-
 	return xengnttab_map_grant_ref(*xgt_handle, domid,
 				       GNTTAB_RESERVED_XENSTORE,
 				       PROT_READ|PROT_WRITE);
tools/xenstored/minios.c +1 -3
index a229954cf4..34f655f20a 100644
@@ -48,13 +48,11 @@ evtchn_port_t get_xenbus_evtchn(void)
 
 void *xenbus_map(void)
 {
-	return xengnttab_map_grant_ref(*xgt_handle, xenbus_master_domid(),
-			GNTTAB_RESERVED_XENSTORE, PROT_READ|PROT_WRITE);
+	return xenstore_buf;
 }
 
 void unmap_xenbus(void *interface)
 {
-	xengnttab_unmap(*xgt_handle, interface, 1);
 }
 
 void early_init(bool live_update, bool dofork, const char *pidfile)
Signature · 1 line
2.50.1
#14 lore raw

Re: [PATCH v4 08/12] tools/xenstored: Remove stubdom special casing

Quoted 38 lines · Jason Andryuk

posix.c and minios.c implement the same named functions serving slightly different purposes.

For xenbus_map()
posix.c maps the local /dev/xen/xsd_kva
minios.c maps dom0 via grant and there is open coding for stub_domid in map_interface.

Change xenbus_map() to map the local domain's interface. The default grant table mapping is performed otherwise.

---
  tools/xenstored/domain.c | 5 -----
  tools/xenstored/minios.c | 4 +---
  2 files changed, 1 insertion(+), 8 deletions(-)
tools/xenstored/domain.c +0 -5
index e9e45ed8e8..ac4ac72f99 100644
@@ -506,11 +506,6 @@ static void *map_interface(domid_t domid)
  	if (domid == xenbus_master_domid())
  		return xenbus_map();
 
-#ifdef __MINIOS__
-	if (domid == stub_domid)
-		return xenstore_buf;
-#endif
-
  	return xengnttab_map_grant_ref(*xgt_handle, domid,
  				       GNTTAB_RESERVED_XENSTORE,
  				       PROT_READ|PROT_WRITE);
tools/xenstored/minios.c +0 -0
index a229954cf4..34f655f20a 100644

tools/xenstored/minios.c evtchn_port_t get_xenbus_evtchn(void) · line 52

Quoted 7 lines
tools/xenstored/minios.c +1 -2
@@ -48,13 +48,11 @@ evtchn_port_t get_xenbus_evtchn(void)
 
  void *xenbus_map(void)
  {
-	return xengnttab_map_grant_ref(*xgt_handle, xenbus_master_domid(),
-			GNTTAB_RESERVED_XENSTORE, PROT_READ|PROT_WRITE);
+	return xenstore_buf;
  }

I think this is correct only after patch 10.

In a Xenstore-stubdom you will swap buffers of dom0 and the stubdom with this patch.

Juergen

#15 lore raw

PATCH v4 9/12 tools/xenstored: Use priv_domid for manual nodes and permission

In reply to Jason Andryuk

Usually, priv_domid == dom0_domid == 0, and that is what is expected. If we rename s/dom0_domid/store_domid/, it seems more likely we want to actually have the priv_domid as the owner.

That leads to follow on changes to ensure that the priv_domid is created first.

---
Will this blow up if priv_domid doesn't exist?

Maybe it would be better to just create these as store_domid.

---
 tools/xenstored/core.c   |  4 ++--
 tools/xenstored/domain.c | 15 ++++++++-------
 2 files changed, 10 insertions(+), 9 deletions(-)
tools/xenstored/core.c +2 -2
index c2f8d20211..19edd75bc3 100644
@@ -2266,7 +2266,7 @@ struct connection *get_connection_by_id(unsigned int conn_id)
 static void manual_node(const char *name, const char *child)
 {
 	struct node *node;
-	struct xs_permissions perms = { .id = dom0_domid,
+	struct xs_permissions perms = { .id = priv_domid,
 					.perms = XS_PERM_NONE };
 
 	node = talloc_zero(NULL, struct node);
@@ -2317,7 +2317,7 @@ void setup_structure(bool live_update)
 		manual_node("/tool/xenstored", NULL);
 		manual_node("@releaseDomain", NULL);
 		manual_node("@introduceDomain", NULL);
-		domain_nbentry_fix(dom0_domid, 5, true);
+		domain_nbentry_fix(priv_domid, 5, true);
 	}
 }
 
tools/xenstored/domain.c +8 -7
index ac4ac72f99..dbeacaa93e 100644
@@ -1009,7 +1009,7 @@ static struct domain *introduce_domain(const void *ctx,
 	struct domain *domain;
 	int rc;
 	struct xenstore_domain_interface *interface;
-	bool is_master_domain = (domid == xenbus_master_domid());
+	bool is_priv_domain = (domid == priv_domid);
 
 	domain = find_or_alloc_domain(ctx, domid);
 	if (!domain)
@@ -1032,13 +1032,13 @@ static struct domain *introduce_domain(const void *ctx,
 		}
 		domain->interface = interface;
 
-		if (is_master_domain)
+		if (is_priv_domain)
 			setup_structure(restore);
 
 		/* Now domain belongs to its connection. */
 		talloc_steal(domain->conn, domain);
 
-		if (!is_master_domain && !restore)
+		if (!is_priv_domain && !restore)
 			fire_special_watches("@introduceDomain");
 	} else {
 		/* Use XS_INTRODUCE for recreating the xenbus event-channel. */
@@ -1295,17 +1295,18 @@ void init_domains(void)
 		domids[nr_domids - 1] = domid;
 
 		if (caps & XENMANAGE_GETDOMSTATE_CAP_XENSTORE) {
+			dom0_domid = domid;
+		}
+
+		if (caps & XENMANAGE_GETDOMSTATE_CAP_CONTROL) {
 			memmove(&domids[1], domids, (nr_domids - 1) * sizeof(*domids));
 			/*
 			 * Local domid must be first to setup structures for
 			 * firing the special watches.
 			 */
 			domids[0] = domid;
-			dom0_domid = domid;
-		}
-
-		if (caps & XENMANAGE_GETDOMSTATE_CAP_CONTROL)
 			priv_domid = domid;
+		}
 	}
 
 	for (unsigned int i = 0; i < nr_domids; i++) {
Signature · 1 line
2.50.1
Reviewed-by on v4 9/12
#16 lore raw

Re: [PATCH v4 09/12] tools/xenstored: Use priv_domid for manual nodes and permission

Quoted 3 lines · Jason Andryuk

Usually, priv_domid == dom0_domid == 0, and that is what is expected. If we rename s/dom0_domid/store_domid/, it seems more likely we want to actually have the priv_domid as the owner.

Yes, I agree.

Quoted 7 lines

That leads to follow on changes to ensure that the priv_domid is created first.

---
Will this blow up if priv_domid doesn't exist?

That won't be a problem. The problematic case will be when priv_domid is never changed due to no doamin having the CONTROL cap, and some random other domU happens to have domid 0.

So maybe priv_domid should be initialized statically to e.g. DOMID_IDLE, as with your init_domains() loop a "normal" dom0 will always have the CONTROL cap and thus will result in priv_domid being set.

Same applies probably to store_domid, but that should be set to priv_domid after init_domains() in case no domain had the XENSTORE cap.

If both aren't detected it should be fine to bail out early.

Quoted 2 lines

Maybe it would be better to just create these as store_domid.

No, reasoning see above. And xenstore-stubdom accesses to Xenstore nodes via the "normal" interfaces shouldn't need any special privileges.

Juergen

#17 lore raw

Re: [PATCH v4 09/12] tools/xenstored: Use priv_domid for manual nodes and permission

Quoted 15 lines · Jürgen Groß
Quoted 3 lines · Jason Andryuk

Usually, priv_domid == dom0_domid == 0, and that is what is expected. If we rename s/dom0_domid/store_domid/, it seems more likely we want to actually have the priv_domid as the owner.

Yes, I agree.

Quoted 7 lines

That leads to follow on changes to ensure that the priv_domid is created first.

---
Will this blow up if priv_domid doesn't exist?

Quoted 7 lines

That won't be a problem. The problematic case will be when priv_domid is never changed due to no doamin having the CONTROL cap, and some random other domU happens to have domid 0.

So maybe priv_domid should be initialized statically to e.g. DOMID_IDLE, as with your init_domains() loop a "normal" dom0 will always have the CONTROL cap and thus will result in priv_domid being set.

There is existing use of DOMID_INVALID, so I'll use that

Quoted 4 lines

Same applies probably to store_domid, but that should be set to priv_domid after init_domains() in case no domain had the XENSTORE cap.

If both aren't detected it should be fine to bail out early.

For our use cases, we want the possibility to run xenstored without a control domain. In that case, I think it would make sense to set priv_domid = store_domid to get xenstored to run.

Quoted 7 lines
Quoted 2 lines

Maybe it would be better to just create these as store_domid.

No, reasoning see above. And xenstore-stubdom accesses to Xenstore nodes via the "normal" interfaces shouldn't need any special privileges.

Thanks, but I held off applying with the priv_domid = store_domid assignment. (Well dom0_domid since it's before the rename).

Regards,
Jason

#18 lore raw

PATCH v4 10/12 tools/xenstored: Rename dom0_domid to store_domid

In reply to Jason Andryuk

The dom0_domid variable is misnamed and conflates purposes. If we have xenstored running in a Linux domain that is not dom0, this variable controls the lookup of /proc/xen/xsd_kva and the event channel.

One implication of this change is that the xenstore domain is not privileged by virtue of considering store_domid as privileged.

Rename to store_domid to better show its purpose.

No functional change.

Add a description of the -m/--master-domid options while doing this.

---
 tools/xenstored/core.c   | 9 ++++++---
 tools/xenstored/core.h   | 6 +++---
 tools/xenstored/domain.c | 8 ++++----
 tools/xenstored/posix.c  | 2 +-
 4 files changed, 14 insertions(+), 11 deletions(-)
tools/xenstored/core.c +6 -3
index 19edd75bc3..981907ec28 100644
@@ -2536,7 +2536,10 @@ static void usage(void)
 "                          allowed timeout candidates are:\n"
 "                          watch-event: time a watch-event is kept pending\n"
 "  -K, --keep-orphans      don't delete nodes owned by a domain when the\n"
-"                          domain is deleted (this is a security risk!)\n");
+"                          domain is deleted (this is a security risk!)\n"
+"  -m, --master-domid      specify the domid of the domain where xenstored\n"
+"                          is running.  defaults to 0\n"
+);
 }
 
 
@@ -2564,7 +2567,7 @@ static struct option options[] = {
 #endif
 	{ NULL, 0, NULL, 0 } };
 
-int dom0_domid = 0;
+int store_domid = 0;
 int dom0_event = 0;
 int priv_domid = 0;
 domid_t stub_domid = DOMID_INVALID;
@@ -2733,7 +2736,7 @@ int main(int argc, char *argv[])
 			dom0_event = get_optval_uint(optarg);
 			break;
 		case 'm':
-			dom0_domid = get_optval_uint(optarg);
+			store_domid = get_optval_uint(optarg);
 			break;
 		case 'p':
 			priv_domid = get_optval_uint(optarg);
tools/xenstored/core.h +3 -3
index 1ba9592d16..d44cca8454 100644
@@ -364,7 +364,7 @@ do {						\
 		trace("tdb: " __VA_ARGS__);	\
 } while (0)
 
-extern int dom0_domid;
+extern int store_domid;
 extern int dom0_event;
 extern int priv_domid;
 extern domid_t stub_domid;
@@ -381,11 +381,11 @@ uint64_t get_now_msec(void);
 void *xenbus_map(void);
 void unmap_xenbus(void *interface);
 
-static inline int xenbus_master_domid(void) { return dom0_domid; }
+static inline int xenbus_master_domid(void) { return store_domid; }
 
 static inline bool domid_is_unprivileged(unsigned int domid)
 {
-	return domid != dom0_domid && domid != priv_domid;
+	return domid != store_domid && domid != priv_domid;
 }
 
 static inline bool domain_is_unprivileged(const struct connection *conn)
tools/xenstored/domain.c +4 -4
index dbeacaa93e..d9144e97a1 100644
@@ -1139,7 +1139,7 @@ static struct domain *onearg_domain(struct connection *conn,
 		return ERR_PTR(-EINVAL);
 
 	domid = atoi(domid_str);
-	if (domid == dom0_domid)
+	if (domid == store_domid)
 		return ERR_PTR(-EINVAL);
 
 	return find_connected_domain(domid);
@@ -1267,7 +1267,7 @@ evtchn_port_t get_domain_evtchn(domid_t domid)
 		return get_xenbus_evtchn();
 	}
 #else
-	if (domid == xenbus_master_domid()) {
+	if (domid == store_domid) {
 		return get_xenbus_evtchn();
 	}
 #endif
@@ -1295,13 +1295,13 @@ void init_domains(void)
 		domids[nr_domids - 1] = domid;
 
 		if (caps & XENMANAGE_GETDOMSTATE_CAP_XENSTORE) {
-			dom0_domid = domid;
+			store_domid = domid;
 		}
 
 		if (caps & XENMANAGE_GETDOMSTATE_CAP_CONTROL) {
 			memmove(&domids[1], domids, (nr_domids - 1) * sizeof(*domids));
 			/*
-			 * Local domid must be first to setup structures for
+			 * priv domid must be first to setup structures for
 			 * firing the special watches.
 			 */
 			domids[0] = domid;
tools/xenstored/posix.c +1 -1
index 6037d739d0..d0622dc05f 100644
@@ -266,7 +266,7 @@ static void accept_connection(int sock)
 	conn = new_connection(&socket_funcs);
 	if (conn) {
 		conn->fd = fd;
-		conn->id = dom0_domid;
+		conn->id = store_domid;
 	} else
 		close(fd);
 }
Signature · 1 line
2.50.1
#19 lore raw

Re: [PATCH v4 10/12] tools/xenstored: Rename dom0_domid to store_domid

Quoted 70 lines · Jason Andryuk

The dom0_domid variable is misnamed and conflates purposes. If we have xenstored running in a Linux domain that is not dom0, this variable controls the lookup of /proc/xen/xsd_kva and the event channel.

One implication of this change is that the xenstore domain is not privileged by virtue of considering store_domid as privileged.

Rename to store_domid to better show its purpose.

No functional change.

Add a description of the -m/--master-domid options while doing this.

---
  tools/xenstored/core.c   | 9 ++++++---
  tools/xenstored/core.h   | 6 +++---
  tools/xenstored/domain.c | 8 ++++----
  tools/xenstored/posix.c  | 2 +-
  4 files changed, 14 insertions(+), 11 deletions(-)
tools/xenstored/core.c +6 -3
index 19edd75bc3..981907ec28 100644
@@ -2536,7 +2536,10 @@ static void usage(void)
  "                          allowed timeout candidates are:\n"
  "                          watch-event: time a watch-event is kept pending\n"
  "  -K, --keep-orphans      don't delete nodes owned by a domain when the\n"
-"                          domain is deleted (this is a security risk!)\n");
+"                          domain is deleted (this is a security risk!)\n"
+"  -m, --master-domid      specify the domid of the domain where xenstored\n"
+"                          is running.  defaults to 0\n"
+);
  }
 
 
@@ -2564,7 +2567,7 @@ static struct option options[] = {
  #endif
  	{ NULL, 0, NULL, 0 } };
 
-int dom0_domid = 0;
+int store_domid = 0;
  int dom0_event = 0;
  int priv_domid = 0;
  domid_t stub_domid = DOMID_INVALID;
@@ -2733,7 +2736,7 @@ int main(int argc, char *argv[])
  			dom0_event = get_optval_uint(optarg);
  			break;
  		case 'm':
-			dom0_domid = get_optval_uint(optarg);
+			store_domid = get_optval_uint(optarg);
  			break;
  		case 'p':
  			priv_domid = get_optval_uint(optarg);
tools/xenstored/core.h +1 -1
index 1ba9592d16..d44cca8454 100644
@@ -364,7 +364,7 @@ do {						\
  		trace("tdb: " __VA_ARGS__);	\
  } while (0)
 
-extern int dom0_domid;
+extern int store_domid;
  extern int dom0_event;
  extern int priv_domid;
  extern domid_t stub_domid;

tools/xenstored/core.h uint64_t get_now_msec(void); · line 384

Quoted 5 lines
tools/xenstored/core.h +1 -1
@@ -381,11 +381,11 @@ uint64_t get_now_msec(void);
  void *xenbus_map(void);
  void unmap_xenbus(void *interface);
 
-static inline int xenbus_master_domid(void) { return dom0_domid; }
+static inline int xenbus_master_domid(void) { return store_domid; }

Maybe just drop xenbus_master_domid() and replace its use cases with store_domid directly?

tools/xenstored/core.h uint64_t get_now_msec(void); · line 388

Quoted 5 lines
tools/xenstored/core.h +1 -1
  static inline bool domid_is_unprivileged(unsigned int domid)
  {
-	return domid != dom0_domid && domid != priv_domid;
+	return domid != store_domid && domid != priv_domid;

I'd say drop the test for store_domid here. I don't think it is appropriate to give xenstore-stubdom special rights.

tools/xenstored/domain.c static struct domain *onearg_domain(struct connection *conn, · line 1142

Quoted 13 lines
tools/xenstored/core.h +0 -0
  }
 
  static inline bool domain_is_unprivileged(const struct connection *conn)
tools/xenstored/domain.c +1 -1
index dbeacaa93e..d9144e97a1 100644
@@ -1139,7 +1139,7 @@ static struct domain *onearg_domain(struct connection *conn,
  		return ERR_PTR(-EINVAL);
 
  	domid = atoi(domid_str);
-	if (domid == dom0_domid)
+	if (domid == store_domid)

And here I'm not sure we want to allow to priv_domid.

There will be noone left who could call XS_RESUME if we'd allow priv_domid to be released. store_domid needs to be kept excluded, too, of course.

tools/xenstored/domain.c evtchn_port_t get_domain_evtchn(domid_t domid) · line 1271

Quoted 10 lines
tools/xenstored/domain.c +1 -1
  		return ERR_PTR(-EINVAL);
 
  	return find_connected_domain(domid);
@@ -1267,7 +1267,7 @@ evtchn_port_t get_domain_evtchn(domid_t domid)
  		return get_xenbus_evtchn();
  	}
  #else
-	if (domid == xenbus_master_domid()) {
+	if (domid == store_domid) {
  		return get_xenbus_evtchn();

And now this is correct. :-)

Quoted 23 lines
tools/xenstored/domain.c +2 -2
  	}
  #endif
@@ -1295,13 +1295,13 @@ void init_domains(void)
  		domids[nr_domids - 1] = domid;
 
  		if (caps & XENMANAGE_GETDOMSTATE_CAP_XENSTORE) {
-			dom0_domid = domid;
+			store_domid = domid;
  		}
 
  		if (caps & XENMANAGE_GETDOMSTATE_CAP_CONTROL) {
  			memmove(&domids[1], domids, (nr_domids - 1) * sizeof(*domids));
  			/*
-			 * Local domid must be first to setup structures for
+			 * priv domid must be first to setup structures for
  			 * firing the special watches.
  			 */
  			domids[0] = domid;
tools/xenstored/posix.c +0 -0
index 6037d739d0..d0622dc05f 100644

tools/xenstored/posix.c static void accept_connection(int sock) · line 272

Quoted 8 lines
tools/xenstored/posix.c +1 -1
@@ -266,7 +266,7 @@ static void accept_connection(int sock)
  	conn = new_connection(&socket_funcs);
  	if (conn) {
  		conn->fd = fd;
-		conn->id = dom0_domid;
+		conn->id = store_domid;
  	} else
  		close(fd);
  }

Juergen

#20 lore raw

PATCH v4 11/12 tools/xenstored: Rename xenbus_master_domid -> xenbus_store_domain

In reply to Jason Andryuk

Rename the helper that access store_domid accordingly.

While doing this, rename the is_store_domain variable.

No functional change.

---
 tools/xenstored/core.h   | 2 +-
 tools/xenstored/domain.c | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
tools/xenstored/core.h +1 -1
index d44cca8454..3e209893f3 100644
@@ -381,7 +381,7 @@ uint64_t get_now_msec(void);
 void *xenbus_map(void);
 void unmap_xenbus(void *interface);
 
-static inline int xenbus_master_domid(void) { return store_domid; }
+static inline int xenbus_store_domid(void) { return store_domid; }
 
 static inline bool domid_is_unprivileged(unsigned int domid)
 {
tools/xenstored/domain.c +2 -2
index d9144e97a1..f1527a6db7 100644
@@ -503,7 +503,7 @@ static const struct interface_funcs domain_funcs = {
 
 static void *map_interface(domid_t domid)
 {
-	if (domid == xenbus_master_domid())
+	if (domid == xenbus_store_domid())
 		return xenbus_map();
 
 	return xengnttab_map_grant_ref(*xgt_handle, domid,
@@ -513,7 +513,7 @@ static void *map_interface(domid_t domid)
 
 static void unmap_interface(domid_t domid, void *interface)
 {
-	if (domid == xenbus_master_domid())
+	if (domid == xenbus_store_domid())
 		unmap_xenbus(interface);
 	else if (domid != stub_domid)
 		xengnttab_unmap(*xgt_handle, interface, 1);
Signature · 1 line
2.50.1
#21 lore raw

Re: [PATCH v4 11/12] tools/xenstored: Rename xenbus_master_domid -> xenbus_store_domain

Quoted 7 lines · Jason Andryuk

Rename the helper that access store_domid accordingly.

While doing this, rename the is_store_domain variable.

No functional change.

With my remark on the previous patch this one can just be dropped.

Juergen

#22 lore raw

PATCH v4 12/12 tools/xenstored: Remove hardcoded implicit path

In reply to Jason Andryuk

Update get_implicit_path to return the correct value for a non-dom0 xenstored domain.

---
 tools/xenstored/domain.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
tools/xenstored/domain.c +6 -1
index f1527a6db7..ae59b6e60b 100644
@@ -1238,11 +1238,13 @@ static int close_xgt_handle(void *_handle)
 	return 0;
 }
 
+static char store_domain_path[] = "/local/domain/65535";
+
 /* Returns the implicit path of a connection (only domains have this) */
 const char *get_implicit_path(const struct connection *conn)
 {
 	if (!conn->domain)
-		return "/local/domain/0";
+		return store_domain_path;
 	return conn->domain->path;
 }
 
@@ -1309,6 +1311,9 @@ void init_domains(void)
 		}
 	}
 
+	snprintf(store_domain_path, sizeof(store_domain_path),
+		 "/local/domain/%u", store_domid);
+
 	for (unsigned int i = 0; i < nr_domids; i++) {
 		evtchn_port_t port;
 		struct domain *domain;
Signature · 1 line
2.50.1