diff mbox

[RFC,XEN,v2,09/15] tools/libacpi: add callbacks to access XenStore

Message ID 20170320000949.24675-10-haozhong.zhang@intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Haozhong Zhang March 20, 2017, 12:09 a.m. UTC
libacpi needs to access information placed in XenStore in order to
load ACPI built by the device model.

Signed-off-by: Haozhong Zhang <haozhong.zhang@intel.com>
---
Cc: Jan Beulich <jbeulich@suse.com>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Cc: Wei Liu <wei.liu2@citrix.com>

Changes in v2:
 * Extract the common part of the existing xenstore_read() and the
   new xenstore_directory() in xenbus.c.
---
 tools/firmware/hvmloader/util.c   | 52 +++++++++++++++++++++++++++++++++++++++
 tools/firmware/hvmloader/util.h   |  9 +++++++
 tools/firmware/hvmloader/xenbus.c | 44 +++++++++++++++++++++++----------
 tools/libacpi/libacpi.h           | 10 ++++++++
 tools/libxl/libxl_x86_acpi.c      | 29 ++++++++++++++++++++++
 5 files changed, 131 insertions(+), 13 deletions(-)
diff mbox

Patch

diff --git a/tools/firmware/hvmloader/util.c b/tools/firmware/hvmloader/util.c
index b2372a75be..ec0de711c2 100644
--- a/tools/firmware/hvmloader/util.c
+++ b/tools/firmware/hvmloader/util.c
@@ -893,6 +893,53 @@  static uint8_t acpi_lapic_id(unsigned cpu)
     return LAPIC_ID(cpu);
 }
 
+static const char *acpi_xs_read(struct acpi_ctxt *ctxt, const char *path)
+{
+    return xenstore_read(path, NULL);
+}
+
+static int acpi_xs_write(struct acpi_ctxt *ctxt,
+                         const char *path, const char *value)
+{
+    return xenstore_write(path, value);
+}
+
+static unsigned int count_strings(const char *strings, unsigned int len)
+{
+    const char *p;
+    unsigned int n;
+
+    for ( p = strings, n = 0; p < strings + len; p++ )
+        if ( *p == '\0' )
+            n++;
+
+    return n;
+}
+
+static char **acpi_xs_directory(struct acpi_ctxt *ctxt,
+                                const char *path, unsigned int *num)
+{
+    const char *strings;
+    char *s, *p, **ret;
+    unsigned int len, n;
+
+    strings = xenstore_directory(path, &len, NULL);
+    if ( !strings )
+        return NULL;
+
+    n = count_strings(strings, len);
+    ret = ctxt->mem_ops.alloc(ctxt, n * sizeof(p) + len, 0);
+    if ( !ret )
+        return NULL;
+    memcpy(&ret[n], strings, len);
+
+    s = (char *)&ret[n];
+    for ( p = s, *num = 0; p < s + len; p += strlen(p) + 1 )
+        ret[(*num)++] = p;
+
+    return ret;
+}
+
 void hvmloader_acpi_build_tables(struct acpi_config *config,
                                  unsigned int physical)
 {
@@ -979,6 +1026,11 @@  void hvmloader_acpi_build_tables(struct acpi_config *config,
 
     ctxt.min_alloc_byte_align = 16;
 
+    ctxt.xs_ops.read = acpi_xs_read;
+    ctxt.xs_ops.write = acpi_xs_write;
+    ctxt.xs_ops.directory = acpi_xs_directory;
+    ctxt.xs_opaque = NULL;
+
     acpi_build_tables(&ctxt, config);
 
     hvm_param_set(HVM_PARAM_VM_GENERATION_ID_ADDR, config->vm_gid_addr);
diff --git a/tools/firmware/hvmloader/util.h b/tools/firmware/hvmloader/util.h
index 6a50dae1eb..ac10a7106a 100644
--- a/tools/firmware/hvmloader/util.h
+++ b/tools/firmware/hvmloader/util.h
@@ -225,6 +225,15 @@  const char *xenstore_read(const char *path, const char *default_resp);
  */
 int xenstore_write(const char *path, const char *value);
 
+/* Read a xenstore directory. Return NULL, or a nul-terminated string
+ * which contains all names of directory entries. Names are separated
+ * by '\0'. The returned string is in a static buffer, so only valid
+ * until the next xenstore/xenbus operation.  If @default_resp is
+ * specified, it is returned in preference to a NULL or empty string
+ * received from xenstore.
+ */
+const char *xenstore_directory(const char *path, uint32_t *len,
+                               const char *default_resp);
 
 /* Get a HVM param.
  */
diff --git a/tools/firmware/hvmloader/xenbus.c b/tools/firmware/hvmloader/xenbus.c
index 448157dcb0..8cad7af019 100644
--- a/tools/firmware/hvmloader/xenbus.c
+++ b/tools/firmware/hvmloader/xenbus.c
@@ -245,24 +245,16 @@  static int xenbus_recv(uint32_t *reply_len, const char **reply_data,
     return 0;
 }
 
-
-/* Read a xenstore key.  Returns a nul-terminated string (even if the XS
- * data wasn't nul-terminated) or NULL.  The returned string is in a
- * static buffer, so only valid until the next xenstore/xenbus operation.
- * If @default_resp is specified, it is returned in preference to a NULL or
- * empty string received from xenstore.
- */
-const char *xenstore_read(const char *path, const char *default_resp)
+static const char *xenstore_read_common(const char *path, uint32_t *len,
+                                        const char *default_resp, bool is_dir)
 {
-    uint32_t len = 0, type = 0;
+    uint32_t type = 0, expected_type = is_dir ? XS_DIRECTORY : XS_READ;
     const char *answer = NULL;
 
-    xenbus_send(XS_READ,
-                path, strlen(path),
-                "", 1, /* nul separator */
+    xenbus_send(expected_type, path, strlen(path), "", 1, /* nul separator */
                 NULL, 0);
 
-    if ( xenbus_recv(&len, &answer, &type) || (type != XS_READ) )
+    if ( xenbus_recv(len, &answer, &type) || type != expected_type )
         answer = NULL;
 
     if ( (default_resp != NULL) && ((answer == NULL) || (*answer == '\0')) )
@@ -272,6 +264,32 @@  const char *xenstore_read(const char *path, const char *default_resp)
     return answer;
 }
 
+/* Read a xenstore key.  Returns a nul-terminated string (even if the XS
+ * data wasn't nul-terminated) or NULL.  The returned string is in a
+ * static buffer, so only valid until the next xenstore/xenbus operation.
+ * If @default_resp is specified, it is returned in preference to a NULL or
+ * empty string received from xenstore.
+ */
+const char *xenstore_read(const char *path, const char *default_resp)
+{
+    uint32_t len = 0;
+
+    return xenstore_read_common(path, &len, default_resp, false);
+}
+
+/* Read a xenstore directory. Return NULL, or a nul-terminated string
+ * which contains all names of directory entries. Names are separated
+ * by '\0'. The returned string is in a static buffer, so only valid
+ * until the next xenstore/xenbus operation.  If @default_resp is
+ * specified, it is returned in preference to a NULL or empty string
+ * received from xenstore.
+ */
+const char *xenstore_directory(const char *path, uint32_t *len,
+                               const char *default_resp)
+{
+    return xenstore_read_common(path, len, default_resp, true);
+}
+
 /* Write a xenstore key.  @value must be a nul-terminated string. Returns
  * zero on success or a xenstore error code on failure.
  */
diff --git a/tools/libacpi/libacpi.h b/tools/libacpi/libacpi.h
index 48acf9583c..645e091f68 100644
--- a/tools/libacpi/libacpi.h
+++ b/tools/libacpi/libacpi.h
@@ -54,6 +54,16 @@  struct acpi_ctxt {
     } mem_ops;
 
     uint32_t min_alloc_byte_align; /* minimum alignment used by mem_ops.alloc */
+
+    struct acpi_xs_ops {
+        const char *(*read)(struct acpi_ctxt *ctxt, const char *path);
+        int (*write)(struct acpi_ctxt *ctxt,
+                     const char *path, const char *value);
+        char **(*directory)(struct acpi_ctxt *ctxt,
+                            const char *path, unsigned int *num);
+    } xs_ops;
+
+    void *xs_opaque;
 };
 
 struct acpi_config {
diff --git a/tools/libxl/libxl_x86_acpi.c b/tools/libxl/libxl_x86_acpi.c
index 2622e03ce6..591a4f44fa 100644
--- a/tools/libxl/libxl_x86_acpi.c
+++ b/tools/libxl/libxl_x86_acpi.c
@@ -93,6 +93,25 @@  static void acpi_mem_free(struct acpi_ctxt *ctxt,
 {
 }
 
+static const char *acpi_xs_read(struct acpi_ctxt *ctxt, const char *path)
+{
+    return libxl__xs_read((libxl__gc *)ctxt->xs_opaque, XBT_NULL, path);
+}
+
+static int acpi_xs_write(struct acpi_ctxt *ctxt,
+                         const char *path, const char *value)
+{
+    return libxl__xs_write_checked((libxl__gc *)ctxt->xs_opaque, XBT_NULL,
+                                   path, value);
+}
+
+static char **acpi_xs_directory(struct acpi_ctxt *ctxt,
+                                const char *path, unsigned int *num)
+{
+    return libxl__xs_directory((libxl__gc *)ctxt->xs_opaque, XBT_NULL,
+                               path, num);
+}
+
 static uint8_t acpi_lapic_id(unsigned cpu)
 {
     return cpu * 2;
@@ -195,6 +214,16 @@  int libxl__dom_load_acpi(libxl__gc *gc,
 
     libxl_ctxt.c.min_alloc_byte_align = 16;
 
+    libxl_ctxt.c.xs_ops.read = acpi_xs_read;
+    libxl_ctxt.c.xs_ops.write = acpi_xs_write;
+    libxl_ctxt.c.xs_ops.directory = acpi_xs_directory;
+    libxl_ctxt.c.xs_opaque = gc;
+
+    libxl_ctxt.c.xs_ops.read = acpi_xs_read;
+    libxl_ctxt.c.xs_ops.write = acpi_xs_write;
+    libxl_ctxt.c.xs_ops.directory = acpi_xs_directory;
+    libxl_ctxt.c.xs_opaque = gc;
+
     rc = init_acpi_config(gc, dom, b_info, &config);
     if (rc) {
         LOG(ERROR, "init_acpi_config failed (rc=%d)", rc);