diff mbox series

[v3,2/5] Mini-OS: get own domid

Message ID 20231121094953.22430-3-jgross@suse.com (mailing list archive)
State New, archived
Headers show
Series Mini-OS: preparations for 9pfs in xenstore-stubdom | expand

Commit Message

Jürgen Groß Nov. 21, 2023, 9:49 a.m. UTC
Get the own domid via creation of a temporary event channel. There is
no "official" way to read the own domid in PV guests, so use the event
channel interface to get it:

- allocate an unbound event channel specifying DOMID_SELF for the
  other end

- read the event channel status which will contain the own domid in
  unbound.dom

- close the event channel

Signed-off-by: Juergen Gross <jgross@suse.com
---
V2:
- new patch
V3:
- rename function to get_domid() (Julien Grall)
Signed-off-by: Juergen Gross <jgross@suse.com>
---
 events.c      | 33 +++++++++++++++++++++++++++++++++
 include/lib.h |  3 +++
 2 files changed, 36 insertions(+)

Comments

Samuel Thibault Nov. 21, 2023, 7:27 p.m. UTC | #1
Juergen Gross, le mar. 21 nov. 2023 10:49:50 +0100, a ecrit:
> Get the own domid via creation of a temporary event channel. There is
> no "official" way to read the own domid in PV guests, so use the event
> channel interface to get it:
> 
> - allocate an unbound event channel specifying DOMID_SELF for the
>   other end
> 
> - read the event channel status which will contain the own domid in
>   unbound.dom
> 
> - close the event channel
> 
> Signed-off-by: Juergen Gross <jgross@suse.com

Reviewed-by: Samuel Thibault <samuel.thibault@ens-lyon.org>

Thanks!

> ---
> V2:
> - new patch
> V3:
> - rename function to get_domid() (Julien Grall)
> Signed-off-by: Juergen Gross <jgross@suse.com>
> ---
>  events.c      | 33 +++++++++++++++++++++++++++++++++
>  include/lib.h |  3 +++
>  2 files changed, 36 insertions(+)
> 
> diff --git a/events.c b/events.c
> index cdae90f4..4683e8e1 100644
> --- a/events.c
> +++ b/events.c
> @@ -261,6 +261,39 @@ int evtchn_get_peercontext(evtchn_port_t local_port, char *ctx, int size)
>      return rc;
>  }
>  
> +/* Replace below when a hypercall is available to get the domid. */
> +domid_t get_domid(void)
> +{
> +    int rc;
> +    domid_t domid = DOMID_INVALID;
> +    evtchn_alloc_unbound_t op;
> +    struct evtchn_status status;
> +    struct evtchn_close close;
> +
> +    op.dom = DOMID_SELF;
> +    op.remote_dom = DOMID_SELF;
> +    rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, &op);
> +    if ( rc )
> +    {
> +        printk("ERROR: alloc_unbound failed with rc=%d", rc);
> +        return domid;
> +    }
> +
> +    status.dom = DOMID_SELF;
> +    status.port = op.port;
> +    rc = HYPERVISOR_event_channel_op(EVTCHNOP_status, &status);
> +    if ( rc )
> +        printk("ERROR: EVTCHNOP_status failed with rc=%d", rc);
> +    else
> +        domid = status.u.unbound.dom;
> +
> +    close.port = op.port;
> +    rc = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
> +    if ( rc )
> +        printk("WARN: close_port %d failed rc=%d. ignored\n", close.port, rc);
> +
> +    return domid;
> +}
>  
>  /*
>   * Local variables:
> diff --git a/include/lib.h b/include/lib.h
> index fd8c36de..dd68985a 100644
> --- a/include/lib.h
> +++ b/include/lib.h
> @@ -154,6 +154,9 @@ do {                                                           \
>  /* Consistency check as much as possible. */
>  void sanity_check(void);
>  
> +/* Get own domid. */
> +domid_t get_domid(void);
> +
>  #ifdef HAVE_LIBC
>  extern struct wait_queue_head event_queue;
>  
> -- 
> 2.35.3
>
diff mbox series

Patch

diff --git a/events.c b/events.c
index cdae90f4..4683e8e1 100644
--- a/events.c
+++ b/events.c
@@ -261,6 +261,39 @@  int evtchn_get_peercontext(evtchn_port_t local_port, char *ctx, int size)
     return rc;
 }
 
+/* Replace below when a hypercall is available to get the domid. */
+domid_t get_domid(void)
+{
+    int rc;
+    domid_t domid = DOMID_INVALID;
+    evtchn_alloc_unbound_t op;
+    struct evtchn_status status;
+    struct evtchn_close close;
+
+    op.dom = DOMID_SELF;
+    op.remote_dom = DOMID_SELF;
+    rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, &op);
+    if ( rc )
+    {
+        printk("ERROR: alloc_unbound failed with rc=%d", rc);
+        return domid;
+    }
+
+    status.dom = DOMID_SELF;
+    status.port = op.port;
+    rc = HYPERVISOR_event_channel_op(EVTCHNOP_status, &status);
+    if ( rc )
+        printk("ERROR: EVTCHNOP_status failed with rc=%d", rc);
+    else
+        domid = status.u.unbound.dom;
+
+    close.port = op.port;
+    rc = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
+    if ( rc )
+        printk("WARN: close_port %d failed rc=%d. ignored\n", close.port, rc);
+
+    return domid;
+}
 
 /*
  * Local variables:
diff --git a/include/lib.h b/include/lib.h
index fd8c36de..dd68985a 100644
--- a/include/lib.h
+++ b/include/lib.h
@@ -154,6 +154,9 @@  do {                                                           \
 /* Consistency check as much as possible. */
 void sanity_check(void);
 
+/* Get own domid. */
+domid_t get_domid(void);
+
 #ifdef HAVE_LIBC
 extern struct wait_queue_head event_queue;