From patchwork Mon Mar 20 00:09:43 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haozhong Zhang X-Patchwork-Id: 9632905 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 BEC896020B for ; Mon, 20 Mar 2017 00:16:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B2A3B27F60 for ; Mon, 20 Mar 2017 00:16:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A797227F9F; Mon, 20 Mar 2017 00:16:05 +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.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID 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 0F52827F8C for ; Mon, 20 Mar 2017 00:16:05 +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 1cpkwv-0006kK-11; Mon, 20 Mar 2017 00:13:25 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cpkwt-0006dO-Hj for xen-devel@lists.xen.org; Mon, 20 Mar 2017 00:13:23 +0000 Received: from [85.158.143.35] by server-6.bemta-6.messagelabs.com id 97/60-15112-2AE1FC85; Mon, 20 Mar 2017 00:13:22 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrNLMWRWlGSWpSXmKPExsVywNykWHeR3Pk IgxlNPBZLPi5mcWD0OLr7N1MAYxRrZl5SfkUCa8bxzuVMBUddKk7tO83awPjGtIuRi4NF4BaT RNOeOywgjpDAdEaJpb9vsHYxcnJICPBKHFk2A8r2k1g85QEziC0k0MsocWOuJojNJqAvseLxQ bAaEQFpiWufLzOC2MwCs5kk7jeGgNjCAoESt/sPgPWyCKhKPPh0hw3E5hWwk7h6dCMTxHx5iQ tXT7GA2JxA8TWXbrND7LKV2H9rGtsERr4FjAyrGDWKU4vKUot0jUz1kooy0zNKchMzc3QNDcz 0clOLixPTU3MSk4r1kvNzNzECA4UBCHYwrloQeIhRkoNJSZRXRfBEhBBfUn5KZUZicUZ8UWlO avEhRhkODiUJ3tm7gXKCRanpqRVpmTnAkIVJS3DwKInwSoGkeYsLEnOLM9MhUqcYFaXEeeeAJ ARAEhmleXBtsDi5xCgrJczLCHSIEE9BalFuZgmq/CtGcQ5GJWHetyBTeDLzSuCmvwJazAS0OP HnEZDFJYkIKakGxiWHM5se7NlW/JG30cT1gGf+2/58laBc/SbZpy2L53ySNtHMXakpt4c39aH T0mA57St74oNKl5/8w7RzYYCh0ryFy50PnNjw69+xzzt9J1X3MLToZ+fr7r/sdSDtqcorv39J 8XJ8DRd45r7MiYzct/FtlQeLz3qerPmtd0Kea2iclP+kvv9InRJLcUaioRZzUXEiAPMHZiqOA gAA X-Env-Sender: haozhong.zhang@intel.com X-Msg-Ref: server-2.tower-21.messagelabs.com!1489968794!49178196!5 X-Originating-IP: [192.55.52.115] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.2.3; banners=-,-,- X-VirusChecked: Checked Received: (qmail 52724 invoked from network); 20 Mar 2017 00:13:21 -0000 Received: from mga14.intel.com (HELO mga14.intel.com) (192.55.52.115) by server-2.tower-21.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 20 Mar 2017 00:13:21 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=intel.com; i=@intel.com; q=dns/txt; s=intel; t=1489968801; x=1521504801; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=fcxVqnKFAfgdi8hxNmoDt8ySQSRTPoFJ/82AMSqAL54=; b=Ff7tiNQUNMHihR1X/P6lV0ioSOrRNZHdCOWgBLK5aEE0iAmrynns2XPa 3iCU+MwWtyRHrZZA+ZXNC+1LdCpodQ==; Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 19 Mar 2017 17:13:21 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.36,191,1486454400"; d="scan'208";a="78799213" Received: from hz-desktop.sh.intel.com (HELO localhost) ([10.239.159.153]) by fmsmga006.fm.intel.com with ESMTP; 19 Mar 2017 17:13:19 -0700 From: Haozhong Zhang To: xen-devel@lists.xen.org Date: Mon, 20 Mar 2017 08:09:43 +0800 Message-Id: <20170320000949.24675-10-haozhong.zhang@intel.com> X-Mailer: git-send-email 2.10.1 In-Reply-To: <20170320000949.24675-1-haozhong.zhang@intel.com> References: <20170320000949.24675-1-haozhong.zhang@intel.com> Cc: Haozhong Zhang , Wei Liu , Andrew Cooper , Ian Jackson , Jan Beulich , Konrad Rzeszutek Wilk , Dan Williams Subject: [Xen-devel] [RFC XEN PATCH v2 09/15] 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 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 --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);