From patchwork Thu Aug 31 09:35:58 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 9931659 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 82D1D603B4 for ; Thu, 31 Aug 2017 09:38:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 754D228884 for ; Thu, 31 Aug 2017 09:38:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6A057288D4; Thu, 31 Aug 2017 09:38:45 +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 B4F7628884 for ; Thu, 31 Aug 2017 09:38:43 +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 1dnLta-00067z-BE; Thu, 31 Aug 2017 09:36:18 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dnLtY-00066f-Ja for xen-devel@lists.xenproject.org; Thu, 31 Aug 2017 09:36:16 +0000 Received: from [85.158.139.211] by server-4.bemta-5.messagelabs.com id 61/4E-02231-F88D7A95; Thu, 31 Aug 2017 09:36:15 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrCIsWRWlGSWpSXmKPExsXitHSDvW7/jeW RBpOniFl83zKZyYHR4/CHKywBjFGsmXlJ+RUJrBmz/j1lK9gWWfF/+w62Bsb1dl2MnBwSAv4S ux5+ZQOx2QR0JKY+vcTaxcjBISKgInF7rwFImFmgWOLv/25mEFtYIEZi+u9mMJtFQFVi7tXZr CA2r4C1xOSdXUwQI+UldrVdBItzCthILFnUwAhiCwHVHFq5jBmiXlDi5MwnLBDzNSVat/9mh7 DlJZq3zmaGqFeRWD91FtsERr5ZSFpmIWmZhaRlASPzKkaN4tSistQiXSNjvaSizPSMktzEzBx dQwNTvdzU4uLE9NScxKRiveT83E2MwFCrZ2Bg3MG4o93vEKMkB5OSKG/YheWRQnxJ+SmVGYnF GfFFpTmpxYcYNTg4BPrWrL7AKMWSl5+XqiTBK3AdqE6wKDU9tSItMwcYDTClEhw8SiK8uSBp3 uKCxNzizHSI1ClGXY4Nq9d/YRICmyElznv+GlCRAEhRRmke3AhYZF5ilJUS5mVkYGAQ4ilILc rNLEGVf8UozsGoJMxbDbKKJzOvBG7TK6AjmICOiPVaCnJESSJCSqqBccL+vBd1lt3zb/4Pt2V wl17gdD/7scmJi48Dph9tu1708SYP18IJOjHzT8ulSB4zWhb6aK+Cz8OF4vXs1VbOR1XmSKWe YcpdnrQvUFm449KuspNng0pj8hcdsl8m5HfbcOesrmc2zm6na+r8XgkGvJrOlxb2M/dzOWtAd KLGTeafWVHnTHfwKLEUZyQaajEXFScCAOSmllTHAgAA X-Env-Sender: prvs=4091fd0e1=Paul.Durrant@citrix.com X-Msg-Ref: server-9.tower-206.messagelabs.com!1504172172!109811052!2 X-Originating-IP: [66.165.176.63] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni42MyA9PiAzMDYwNDg=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 39590 invoked from network); 31 Aug 2017 09:36:15 -0000 Received: from smtp02.citrix.com (HELO SMTP02.CITRIX.COM) (66.165.176.63) by server-9.tower-206.messagelabs.com with RC4-SHA encrypted SMTP; 31 Aug 2017 09:36:15 -0000 X-IronPort-AV: E=Sophos;i="5.41,451,1498521600"; d="scan'208";a="446032830" From: Paul Durrant To: Date: Thu, 31 Aug 2017 10:35:58 +0100 Message-ID: <20170831093605.2757-6-paul.durrant@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170831093605.2757-1-paul.durrant@citrix.com> References: <20170831093605.2757-1-paul.durrant@citrix.com> MIME-Version: 1.0 Cc: Wei Liu , Paul Durrant , Ian Jackson Subject: [Xen-devel] [PATCH v3 05/12] tools/libxenctrl: use new xenforeignmemory API to seed grant table 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: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP A previous patch added support for priv-mapping guest resources directly (rather than having to foreign-map, which requires P2M modification for HVM guests). This patch makes use of the new API to seed the guest grant table unless the underlying infrastructure (i.e. privcmd) doesn't support it, in which case the old scheme is used. NOTE: The call to xc_dom_gnttab_hvm_seed() in hvm_build_set_params() was actually unnecessary, as the grant table has already been seeded by a prior call to xc_dom_gnttab_init() made by libxl__build_dom(). Signed-off-by: Paul Durrant Acked-by: Marek Marczykowski-Górecki Reviewed-by: Roger Pau Monné --- Cc: Ian Jackson Cc: Wei Liu v3: - Introduced xc_dom_set_gnttab_entry() to avoid duplicated code. --- tools/libxc/include/xc_dom.h | 8 +-- tools/libxc/xc_dom_boot.c | 118 ++++++++++++++++++++++++++---------- tools/libxc/xc_sr_restore_x86_hvm.c | 10 +-- tools/libxc/xc_sr_restore_x86_pv.c | 2 +- tools/libxl/libxl_dom.c | 1 - tools/python/xen/lowlevel/xc/xc.c | 6 +- 6 files changed, 96 insertions(+), 49 deletions(-) diff --git a/tools/libxc/include/xc_dom.h b/tools/libxc/include/xc_dom.h index ce47058c41..d6ca0a8680 100644 --- a/tools/libxc/include/xc_dom.h +++ b/tools/libxc/include/xc_dom.h @@ -323,12 +323,8 @@ void *xc_dom_boot_domU_map(struct xc_dom_image *dom, xen_pfn_t pfn, int xc_dom_boot_image(struct xc_dom_image *dom); int xc_dom_compat_check(struct xc_dom_image *dom); int xc_dom_gnttab_init(struct xc_dom_image *dom); -int xc_dom_gnttab_hvm_seed(xc_interface *xch, domid_t domid, - xen_pfn_t console_gmfn, - xen_pfn_t xenstore_gmfn, - domid_t console_domid, - domid_t xenstore_domid); -int xc_dom_gnttab_seed(xc_interface *xch, domid_t domid, +int xc_dom_gnttab_seed(xc_interface *xch, domid_t guest_domid, + bool is_hvm, xen_pfn_t console_gmfn, xen_pfn_t xenstore_gmfn, domid_t console_domid, diff --git a/tools/libxc/xc_dom_boot.c b/tools/libxc/xc_dom_boot.c index c3b44dd399..e0c80c9036 100644 --- a/tools/libxc/xc_dom_boot.c +++ b/tools/libxc/xc_dom_boot.c @@ -280,11 +280,29 @@ static xen_pfn_t xc_dom_gnttab_setup(xc_interface *xch, domid_t domid) return gmfn; } -int xc_dom_gnttab_seed(xc_interface *xch, domid_t domid, - xen_pfn_t console_gmfn, - xen_pfn_t xenstore_gmfn, - domid_t console_domid, - domid_t xenstore_domid) +static void xc_dom_set_gnttab_entry(xc_interface *xch, + grant_entry_v1_t *gnttab, + unsigned int idx, + domid_t guest_domid, + domid_t backend_domid, + xen_pfn_t backend_gmfn) +{ + if ( guest_domid == backend_domid || backend_gmfn == -1) + return; + + xc_dom_printf(xch, "%s: [%u] -> 0x%"PRI_xen_pfn, + __FUNCTION__, idx, backend_gmfn); + + gnttab[idx].flags = GTF_permit_access; + gnttab[idx].domid = backend_domid; + gnttab[idx].frame = backend_gmfn; +} + +static int compat_gnttab_seed(xc_interface *xch, domid_t domid, + xen_pfn_t console_gmfn, + xen_pfn_t xenstore_gmfn, + domid_t console_domid, + domid_t xenstore_domid) { xen_pfn_t gnttab_gmfn; @@ -308,18 +326,10 @@ int xc_dom_gnttab_seed(xc_interface *xch, domid_t domid, return -1; } - if ( domid != console_domid && console_gmfn != -1) - { - gnttab[GNTTAB_RESERVED_CONSOLE].flags = GTF_permit_access; - gnttab[GNTTAB_RESERVED_CONSOLE].domid = console_domid; - gnttab[GNTTAB_RESERVED_CONSOLE].frame = console_gmfn; - } - if ( domid != xenstore_domid && xenstore_gmfn != -1) - { - gnttab[GNTTAB_RESERVED_XENSTORE].flags = GTF_permit_access; - gnttab[GNTTAB_RESERVED_XENSTORE].domid = xenstore_domid; - gnttab[GNTTAB_RESERVED_XENSTORE].frame = xenstore_gmfn; - } + xc_dom_set_gnttab_entry(xch, gnttab, GNTTAB_RESERVED_CONSOLE, + domid, console_domid, console_gmfn); + xc_dom_set_gnttab_entry(xch, gnttab, GNTTAB_RESERVED_XENSTORE, + domid, xenstore_domid, xenstore_gmfn); if ( munmap(gnttab, PAGE_SIZE) == -1 ) { @@ -337,11 +347,11 @@ int xc_dom_gnttab_seed(xc_interface *xch, domid_t domid, return 0; } -int xc_dom_gnttab_hvm_seed(xc_interface *xch, domid_t domid, - xen_pfn_t console_gpfn, - xen_pfn_t xenstore_gpfn, - domid_t console_domid, - domid_t xenstore_domid) +static int compat_gnttab_hvm_seed(xc_interface *xch, domid_t domid, + xen_pfn_t console_gpfn, + xen_pfn_t xenstore_gpfn, + domid_t console_domid, + domid_t xenstore_domid) { int rc; xen_pfn_t scratch_gpfn; @@ -380,7 +390,7 @@ int xc_dom_gnttab_hvm_seed(xc_interface *xch, domid_t domid, return -1; } - rc = xc_dom_gnttab_seed(xch, domid, + rc = compat_gnttab_seed(xch, domid, console_gpfn, xenstore_gpfn, console_domid, xenstore_domid); if (rc != 0) @@ -405,18 +415,60 @@ int xc_dom_gnttab_hvm_seed(xc_interface *xch, domid_t domid, return 0; } -int xc_dom_gnttab_init(struct xc_dom_image *dom) +int xc_dom_gnttab_seed(xc_interface *xch, domid_t guest_domid, + bool is_hvm, xen_pfn_t console_gmfn, + xen_pfn_t xenstore_gmfn, domid_t console_domid, + domid_t xenstore_domid) { - if ( xc_dom_translated(dom) ) { - return xc_dom_gnttab_hvm_seed(dom->xch, dom->guest_domid, - dom->console_pfn, dom->xenstore_pfn, - dom->console_domid, dom->xenstore_domid); - } else { - return xc_dom_gnttab_seed(dom->xch, dom->guest_domid, - xc_dom_p2m(dom, dom->console_pfn), - xc_dom_p2m(dom, dom->xenstore_pfn), - dom->console_domid, dom->xenstore_domid); + xenforeignmemory_handle* fmem = xch->fmem; + xenforeignmemory_resource_handle *fres; + void *addr = NULL; + + fres = xenforeignmemory_map_resource(fmem, guest_domid, + XENMEM_resource_grant_table, + 0, 0, 1, + &addr, PROT_READ | PROT_WRITE, 0); + if ( !fres ) + { + if ( errno == EOPNOTSUPP ) + return is_hvm ? + compat_gnttab_hvm_seed(xch, guest_domid, + console_gmfn, xenstore_gmfn, + console_domid, xenstore_domid) : + compat_gnttab_seed(xch, guest_domid, + console_gmfn, xenstore_gmfn, + console_domid, xenstore_domid); + + xc_dom_panic(xch, XC_INTERNAL_ERROR, + "%s: failed to acquire grant table " + "[errno=%d]\n", + __FUNCTION__, errno); + return -1; } + + xc_dom_set_gnttab_entry(xch, addr, GNTTAB_RESERVED_CONSOLE, + guest_domid, console_domid, console_gmfn); + xc_dom_set_gnttab_entry(xch, addr, GNTTAB_RESERVED_XENSTORE, + guest_domid, xenstore_domid, xenstore_gmfn); + + xenforeignmemory_unmap_resource(fmem, fres); + + return 0; +} + +int xc_dom_gnttab_init(struct xc_dom_image *dom) +{ + xc_interface *xch = dom->xch; + domid_t guest_domid = dom->guest_domid; + bool is_hvm = xc_dom_translated(dom); + xen_pfn_t console_gmfn = xc_dom_p2m(dom, dom->console_pfn); + xen_pfn_t xenstore_gmfn = xc_dom_p2m(dom, dom->xenstore_pfn); + domid_t console_domid = dom->console_domid; + domid_t xenstore_domid = dom->xenstore_domid; + + return xc_dom_gnttab_seed(xch, guest_domid, is_hvm, + console_gmfn, xenstore_gmfn, + console_domid, xenstore_domid); } /* diff --git a/tools/libxc/xc_sr_restore_x86_hvm.c b/tools/libxc/xc_sr_restore_x86_hvm.c index 1dca85354a..a5c661da8f 100644 --- a/tools/libxc/xc_sr_restore_x86_hvm.c +++ b/tools/libxc/xc_sr_restore_x86_hvm.c @@ -207,11 +207,11 @@ static int x86_hvm_stream_complete(struct xc_sr_context *ctx) return rc; } - rc = xc_dom_gnttab_hvm_seed(xch, ctx->domid, - ctx->restore.console_gfn, - ctx->restore.xenstore_gfn, - ctx->restore.console_domid, - ctx->restore.xenstore_domid); + rc = xc_dom_gnttab_seed(xch, ctx->domid, true, + ctx->restore.console_gfn, + ctx->restore.xenstore_gfn, + ctx->restore.console_domid, + ctx->restore.xenstore_domid); if ( rc ) { PERROR("Failed to seed grant table"); diff --git a/tools/libxc/xc_sr_restore_x86_pv.c b/tools/libxc/xc_sr_restore_x86_pv.c index 50e25c162c..10635d436b 100644 --- a/tools/libxc/xc_sr_restore_x86_pv.c +++ b/tools/libxc/xc_sr_restore_x86_pv.c @@ -1104,7 +1104,7 @@ static int x86_pv_stream_complete(struct xc_sr_context *ctx) if ( rc ) return rc; - rc = xc_dom_gnttab_seed(xch, ctx->domid, + rc = xc_dom_gnttab_seed(xch, ctx->domid, false, ctx->restore.console_gfn, ctx->restore.xenstore_gfn, ctx->restore.console_domid, diff --git a/tools/libxl/libxl_dom.c b/tools/libxl/libxl_dom.c index f54fd49a73..0d3e462c12 100644 --- a/tools/libxl/libxl_dom.c +++ b/tools/libxl/libxl_dom.c @@ -851,7 +851,6 @@ static int hvm_build_set_params(xc_interface *handle, uint32_t domid, *store_mfn = str_mfn; *console_mfn = cons_mfn; - xc_dom_gnttab_hvm_seed(handle, domid, *console_mfn, *store_mfn, console_domid, store_domid); return 0; } diff --git a/tools/python/xen/lowlevel/xc/xc.c b/tools/python/xen/lowlevel/xc/xc.c index aa9f8e4d9e..583ab52a6f 100644 --- a/tools/python/xen/lowlevel/xc/xc.c +++ b/tools/python/xen/lowlevel/xc/xc.c @@ -800,9 +800,9 @@ static PyObject *pyxc_gnttab_hvm_seed(XcObject *self, &console_domid, &xenstore_domid) ) return NULL; - if ( xc_dom_gnttab_hvm_seed(self->xc_handle, dom, - console_gmfn, xenstore_gmfn, - console_domid, xenstore_domid) != 0 ) + if ( xc_dom_gnttab_seed(self->xc_handle, dom, true, + console_gmfn, xenstore_gmfn, + console_domid, xenstore_domid) != 0 ) return pyxc_error_to_exception(self->xc_handle); return Py_None;