From patchwork Mon Sep 11 04:38:13 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haozhong Zhang X-Patchwork-Id: 9946593 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id CE6106035D for ; Mon, 11 Sep 2017 04:41:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C2307289DE for ; Mon, 11 Sep 2017 04:41:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B553A28AD7; Mon, 11 Sep 2017 04:41:55 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id E92AD289DE for ; Mon, 11 Sep 2017 04:41:54 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1drGVp-0002fM-96; Mon, 11 Sep 2017 04:39:57 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1drGVo-0002XI-Oz for xen-devel@lists.xen.org; Mon, 11 Sep 2017 04:39:56 +0000 Received: from [193.109.254.147] by server-4.bemta-6.messagelabs.com id 09/A5-03283-C9316B95; Mon, 11 Sep 2017 04:39:56 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrDLMWRWlGSWpSXmKPExsXS1tYhojtbeFu kwao9JhZLPi5mcWD0OLr7N1MAYxRrZl5SfkUCa8aEpmmMBX+cKk6eaGJvYJxt0sXIxSEkMJ1R Yvfm/8xdjJwcEgK8EkeWzWCFsAMkWjZOZYEo6mWUmDjvMDtIgk1AX2LF44NgRSIC0hLXPl9mB CliFjjFJLF+32QWkISwQKDEtdYnYA0sAqoS0879YgOxeQXsJJ7N+8YCsUFeYlfbRbBBnEDxgy /fgV0hJGArseD0AtYJjLwLGBlWMWoUpxaVpRbpGpnrJRVlpmeU5CZm5ugaGpjp5aYWFyemp+Y kJhXrJefnbmIEhgQDEOxgXLw28BCjJAeTkijvu+NbIoX4kvJTKjMSizPii0pzUosPMcpwcChJ 8KoIbYsUEixKTU+tSMvMAQYnTFqCg0dJhDcKJM1bXJCYW5yZDpE6xajL0XHz7h8mIZa8/LxUK XFeOZAiAZCijNI8uBGwSLnEKCslzMsIdJQQT0FqUW5mCar8K0ZxDkYlYd4IkCk8mXklcJteAR 3BBHQEz6UtIEeUJCKkpBoYLfats323fGajeFvSzBbu/suNd9slIpQaRSWv6b/pLO1cofA98FX 3balSgcnRV/QD4r41iP6qnOJY+PfdtwcBwuUnHsU/2zT38dG1wvHP363csXJpSvceyb/Nq2tv 3mTlsmK6GBlq5+gwtfT0Ps1DSxffOsen4c96xv1UTwYj1xL+F3bzNZImKrEUZyQaajEXFScCA GZtxn+PAgAA X-Env-Sender: haozhong.zhang@intel.com X-Msg-Ref: server-2.tower-27.messagelabs.com!1505104735!56506342!27 X-Originating-IP: [134.134.136.20] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogMTM0LjEzNC4xMzYuMjAgPT4gMzU1MzU4\n X-StarScan-Received: X-StarScan-Version: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 51440 invoked from network); 11 Sep 2017 04:39:55 -0000 Received: from mga02.intel.com (HELO mga02.intel.com) (134.134.136.20) by server-2.tower-27.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 11 Sep 2017 04:39:55 -0000 Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 10 Sep 2017 21:39:54 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos; i="5.42,376,1500966000"; d="scan'208"; a="1217078570" Received: from hz-desktop.sh.intel.com (HELO localhost) ([10.239.159.142]) by fmsmga002.fm.intel.com with ESMTP; 10 Sep 2017 21:39:53 -0700 From: Haozhong Zhang To: xen-devel@lists.xen.org Date: Mon, 11 Sep 2017 12:38:13 +0800 Message-Id: <20170911043820.14617-33-haozhong.zhang@intel.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170911043820.14617-1-haozhong.zhang@intel.com> References: <20170911043820.14617-1-haozhong.zhang@intel.com> Cc: Haozhong Zhang , Wei Liu , Andrew Cooper , Ian Jackson , Jan Beulich , Chao Peng , Dan Williams Subject: [Xen-devel] [RFC XEN PATCH v3 32/39] tools/libacpi: add callbacks to access XenStore X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP libacpi needs to access information placed in XenStore in order to load ACPI built by the device model. Signed-off-by: Haozhong Zhang --- Cc: Jan Beulich Cc: Andrew Cooper Cc: Ian Jackson Cc: Wei Liu --- 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 | 24 ++++++++++++++++++ 5 files changed, 126 insertions(+), 13 deletions(-) diff --git a/tools/firmware/hvmloader/util.c b/tools/firmware/hvmloader/util.c index 2f8a4654b0..5b8a4ee9d0 100644 --- a/tools/firmware/hvmloader/util.c +++ b/tools/firmware/hvmloader/util.c @@ -893,6 +893,53 @@ static uint32_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) { @@ -998,6 +1045,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 e9fe6c6e79..37e62d93c0 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 2b89a56fce..387c0971e1 100644 --- a/tools/firmware/hvmloader/xenbus.c +++ b/tools/firmware/hvmloader/xenbus.c @@ -257,24 +257,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')) ) @@ -284,6 +276,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 f5a1c384bc..ab86a35509 100644 --- a/tools/libacpi/libacpi.h +++ b/tools/libacpi/libacpi.h @@ -55,6 +55,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 b14136949c..cbfd9a373c 100644 --- a/tools/libxl/libxl_x86_acpi.c +++ b/tools/libxl/libxl_x86_acpi.c @@ -98,6 +98,25 @@ static uint32_t acpi_lapic_id(unsigned cpu) return cpu * 2; } +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 int init_acpi_config(libxl__gc *gc, struct xc_dom_image *dom, const libxl_domain_build_info *b_info, @@ -195,6 +214,11 @@ 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; + rc = init_acpi_config(gc, dom, b_info, &config); if (rc) { LOG(ERROR, "init_acpi_config failed (rc=%d)", rc);