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

Jason Andryuk posted 12 patches 3 months, 1 week ago
There is a newer version of this series
[PATCH v4 06/12] tools/xenstored: Add get_domain_evtchn() to find evtchn
Posted by Jason Andryuk 3 months, 1 week ago
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.

Signed-off-by: Jason Andryuk <jason.andryuk@amd.com>
---
 tools/xenstored/domain.c | 38 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)

diff --git a/tools/xenstored/domain.c b/tools/xenstored/domain.c
index 1241f8c73e..1c52254ba8 100644
--- a/tools/xenstored/domain.c
+++ b/tools/xenstored/domain.c
@@ -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");
 
-- 
2.50.1
Re: [PATCH v4 06/12] tools/xenstored: Add get_domain_evtchn() to find evtchn
Posted by Jürgen Groß 3 months, 1 week ago
On 25.07.25 04:28, Jason Andryuk wrote:
> 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.
> 
> Signed-off-by: Jason Andryuk <jason.andryuk@amd.com>
> ---
>   tools/xenstored/domain.c | 38 ++++++++++++++++++++++++++++++++++++--
>   1 file changed, 36 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/xenstored/domain.c b/tools/xenstored/domain.c
> index 1241f8c73e..1c52254ba8 100644
> --- a/tools/xenstored/domain.c
> +++ b/tools/xenstored/domain.c
> @@ -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;
> +}
> +

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.

>   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");

s/dom0/stubdom/

> +
> +	stubdom = introduce_domain(NULL, stub_domid, port, false);
>   	if (!stubdom)
>   		barf_perror("Failed to initialize stubdom");
>   

Juergen
Re: [PATCH v4 06/12] tools/xenstored: Add get_domain_evtchn() to find evtchn
Posted by Jason Andryuk 3 months, 1 week ago
On 2025-07-25 02:41, Jürgen Groß wrote:
> On 25.07.25 04:28, Jason Andryuk wrote:
>> 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.
>>
>> Signed-off-by: Jason Andryuk <jason.andryuk@amd.com>
>> ---
>>   tools/xenstored/domain.c | 38 ++++++++++++++++++++++++++++++++++++--
>>   1 file changed, 36 insertions(+), 2 deletions(-)
>>
>> diff --git a/tools/xenstored/domain.c b/tools/xenstored/domain.c
>> index 1241f8c73e..1c52254ba8 100644
>> --- a/tools/xenstored/domain.c
>> +++ b/tools/xenstored/domain.c
>> @@ -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;
>> +}
>> +
> 
> 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.

>>   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");
> 
> s/dom0/stubdom/

Thanks,
Jason