From patchwork Tue Sep 12 16:10:05 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 9949579 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 4DEAB60360 for ; Tue, 12 Sep 2017 16:12:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 39BED28F01 for ; Tue, 12 Sep 2017 16:12:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2E71829031; Tue, 12 Sep 2017 16:12:46 +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 A458028F01 for ; Tue, 12 Sep 2017 16:12:44 +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 1drnlZ-0007N7-Tc; Tue, 12 Sep 2017 16:10:25 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1drnlY-0007JO-5R for xen-devel@lists.xenproject.org; Tue, 12 Sep 2017 16:10:24 +0000 Received: from [85.158.139.211] by server-12.bemta-5.messagelabs.com id A3/E5-30935-FE608B95; Tue, 12 Sep 2017 16:10:23 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFupnkeJIrShJLcpLzFFi42JxWrrBXvcd245 Ig/lXzSy+b5nM5MDocfjDFZYAxijWzLyk/IoE1owVC/+xFHQGVfTufsLSwDjbtouRk0NCwF/i 4Yw9jCA2m4COxNSnl1i7GDk4RARUJG7vNQAxmQXKJTom1IJUCAuESsx8fIgJxGYRUJXY1/mZG cTmFbCR+LjtKzPERHmJXW0XWUFsTgFbiXsn3oJNFwKqefBiExOErSKxfuosNoheQYmTM5+wgN jMAhISB1+8YJ7AyDsLSWoWktQCRqZVjBrFqUVlqUW6hsZ6SUWZ6RkluYmZObqGBqZ6uanFxYn pqTmJScV6yfm5mxiBgcMABDsY/23zPMQoycGkJMqrfH97pBBfUn5KZUZicUZ8UWlOavEhRhkO DiUJ3kbWHZFCgkWp6akVaZk5wBCGSUtw8CiJ8N5lAUrzFhck5hZnpkOkTjHqcnTcvPuHSYglL z8vVUqc9zLIDAGQoozSPLgRsHi6xCgrJczLCHSUEE9BalFuZgmq/CtGcQ5GJWHe2SBTeDLzSu A2vQI6ggnoCJ5LW0COKElESEk1MPJ+OMz9ayeb+bcKzWXux95qewT+Z2PyZ9DZUF9XckTAdJM Sy8XZ1y44b37H5MX/UO71lLsaixd0MpnuvnHMwPSiV9eNpy4FEefaElhNPP6XndcSONLWGrl7 zkVjmQz5bxm6MgLny6pjj+oa5l1K36s6/+M6PY+Pd9RXPzQ4/UymO8/94TUTdiWW4oxEQy3mo uJEAIMHlkuiAgAA X-Env-Sender: prvs=421499f3a=Paul.Durrant@citrix.com X-Msg-Ref: server-16.tower-206.messagelabs.com!1505232618!91187079!4 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 41377 invoked from network); 12 Sep 2017 16:10:22 -0000 Received: from smtp02.citrix.com (HELO SMTP02.CITRIX.COM) (66.165.176.63) by server-16.tower-206.messagelabs.com with RC4-SHA encrypted SMTP; 12 Sep 2017 16:10:22 -0000 X-IronPort-AV: E=Sophos;i="5.42,383,1500940800"; d="scan'208";a="447576637" From: Paul Durrant To: Date: Tue, 12 Sep 2017 17:10:05 +0100 Message-ID: <20170912161015.24948-3-paul.durrant@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170912161015.24948-1-paul.durrant@citrix.com> References: <20170912161015.24948-1-paul.durrant@citrix.com> MIME-Version: 1.0 Cc: Andrew Cooper , Paul Durrant , Jan Beulich Subject: [Xen-devel] [PATCH v6 02/12] x86/mm: add HYPERVISOR_memory_op to acquire guest resources 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 Certain memory resources associated with a guest are not necessarily present in the guest P2M and so are not necessarily available to be foreign-mapped by a tools domain unless they are inserted, which risks shattering a super-page mapping. This patch adds a new memory op to allow such a resource to be priv-mapped directly, by either a PV or HVM tools domain: grant table frames. NOTE: Whilst the new op is not intrinsicly specific to the x86 architecture, I have no means to test it on an ARM platform and so cannot verify that it functions correctly. Hence it is currently only implemented for x86. Signed-off-by: Paul Durrant Acked-by: George Dunlap Reviewed-by: Wei Liu --- Cc: Jan Beulich Cc: Andrew Cooper v5: - Switched __copy_to/from_guest_offset() to copy_to/from_guest_offset(). --- xen/arch/x86/mm.c | 111 ++++++++++++++++++++++++++++++++++++++++++ xen/arch/x86/mm/p2m.c | 3 +- xen/common/grant_table.c | 56 ++++++++++++++------- xen/include/asm-x86/p2m.h | 3 ++ xen/include/public/memory.h | 38 ++++++++++++++- xen/include/xen/grant_table.h | 1 + 6 files changed, 191 insertions(+), 21 deletions(-) diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index cb0189efae..c8f50f3bb0 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -4768,6 +4768,107 @@ int xenmem_add_to_physmap_one( return rc; } +static int xenmem_acquire_grant_table(struct domain *d, + unsigned long frame, + unsigned long nr_frames, + unsigned long mfn_list[]) +{ + unsigned int i; + + /* + * Iterate through the list backwards so that gnttab_get_frame() is + * first called for the highest numbered frame. This means that the + * out-of-bounds check will be done on the first iteration and, if + * the table needs to grow, it will only grow once. + */ + i = nr_frames; + while ( i-- != 0 ) + { + mfn_t mfn = gnttab_get_frame(d, frame + i); + + if ( mfn_eq(mfn, INVALID_MFN) ) + return -EINVAL; + + mfn_list[i] = mfn_x(mfn); + } + + return 0; +} + +static int xenmem_acquire_resource(xen_mem_acquire_resource_t *xmar) +{ + struct domain *d, *currd = current->domain; + unsigned long *mfn_list; + int rc; + + if ( xmar->nr_frames == 0 ) + return -EINVAL; + + d = rcu_lock_domain_by_any_id(xmar->domid); + if ( d == NULL ) + return -ESRCH; + + rc = xsm_domain_memory_map(XSM_TARGET, d); + if ( rc ) + goto out; + + mfn_list = xmalloc_array(unsigned long, xmar->nr_frames); + + rc = -ENOMEM; + if ( !mfn_list ) + goto out; + + switch ( xmar->type ) + { + case XENMEM_resource_grant_table: + rc = -EINVAL; + if ( xmar->id ) /* must be zero for grant_table */ + break; + + rc = xenmem_acquire_grant_table(d, xmar->frame, xmar->nr_frames, + mfn_list); + break; + + default: + rc = -EOPNOTSUPP; + break; + } + + if ( rc ) + goto free_and_out; + + if ( !paging_mode_translate(currd) ) + { + if ( copy_to_guest_offset(xmar->gmfn_list, 0, mfn_list, + xmar->nr_frames) ) + rc = -EFAULT; + } + else + { + unsigned int i; + + for ( i = 0; i < xmar->nr_frames; i++ ) + { + xen_pfn_t gfn; + + rc = -EFAULT; + if ( copy_from_guest_offset(&gfn, xmar->gmfn_list, i, 1) ) + goto free_and_out; + + rc = set_foreign_p2m_entry(currd, gfn, _mfn(mfn_list[i])); + if ( rc ) + goto free_and_out; + } + } + + free_and_out: + xfree(mfn_list); + + out: + rcu_unlock_domain(d); + return rc; +} + long arch_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) { int rc; @@ -4990,6 +5091,16 @@ long arch_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) return rc; } + case XENMEM_acquire_resource: + { + xen_mem_acquire_resource_t xmar; + + if ( copy_from_guest(&xmar, arg, 1) ) + return -EFAULT; + + return xenmem_acquire_resource(&xmar); + } + default: return subarch_memory_op(cmd, arg); } diff --git a/xen/arch/x86/mm/p2m.c b/xen/arch/x86/mm/p2m.c index 0b479105b9..d0f8fc249b 100644 --- a/xen/arch/x86/mm/p2m.c +++ b/xen/arch/x86/mm/p2m.c @@ -1121,8 +1121,7 @@ static int set_typed_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn, } /* Set foreign mfn in the given guest's p2m table. */ -static int set_foreign_p2m_entry(struct domain *d, unsigned long gfn, - mfn_t mfn) +int set_foreign_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn) { return set_typed_p2m_entry(d, gfn, mfn, PAGE_ORDER_4K, p2m_map_foreign, p2m_get_hostp2m(d)->default_access); diff --git a/xen/common/grant_table.c b/xen/common/grant_table.c index 9a4d335ee0..dfd00a9432 100644 --- a/xen/common/grant_table.c +++ b/xen/common/grant_table.c @@ -3607,38 +3607,58 @@ int mem_sharing_gref_to_gfn(struct grant_table *gt, grant_ref_t ref, } #endif -int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn, - mfn_t *mfn) -{ - int rc = 0; - grant_write_lock(d->grant_table); +static mfn_t gnttab_get_frame_locked(struct domain *d, unsigned long idx) +{ + struct grant_table *gt = d->grant_table; + mfn_t mfn = INVALID_MFN; - if ( d->grant_table->gt_version == 0 ) - d->grant_table->gt_version = 1; + if ( gt->gt_version == 0 ) + gt->gt_version = 1; - if ( d->grant_table->gt_version == 2 && + if ( gt->gt_version == 2 && (idx & XENMAPIDX_grant_table_status) ) { idx &= ~XENMAPIDX_grant_table_status; - if ( idx < nr_status_frames(d->grant_table) ) - *mfn = _mfn(virt_to_mfn(d->grant_table->status[idx])); - else - rc = -EINVAL; + if ( idx < nr_status_frames(gt) ) + mfn = _mfn(virt_to_mfn(gt->status[idx])); } else { - if ( (idx >= nr_grant_frames(d->grant_table)) && + if ( (idx >= nr_grant_frames(gt)) && (idx < max_grant_frames) ) gnttab_grow_table(d, idx + 1); - if ( idx < nr_grant_frames(d->grant_table) ) - *mfn = _mfn(virt_to_mfn(d->grant_table->shared_raw[idx])); - else - rc = -EINVAL; + if ( idx < nr_grant_frames(gt) ) + mfn = _mfn(virt_to_mfn(gt->shared_raw[idx])); } - gnttab_set_frame_gfn(d, idx, gfn); + return mfn; +} + +mfn_t gnttab_get_frame(struct domain *d, unsigned long idx) +{ + mfn_t mfn; + + grant_write_lock(d->grant_table); + mfn = gnttab_get_frame_locked(d, idx); + grant_write_unlock(d->grant_table); + + return mfn; +} + +int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn, + mfn_t *mfn) +{ + int rc = 0; + + grant_write_lock(d->grant_table); + + *mfn = gnttab_get_frame_locked(d, idx); + if ( mfn_eq(*mfn, INVALID_MFN) ) + rc = -EINVAL; + else + gnttab_set_frame_gfn(d, idx, gfn); grant_write_unlock(d->grant_table); diff --git a/xen/include/asm-x86/p2m.h b/xen/include/asm-x86/p2m.h index 10cdfc09a9..4eff0458bc 100644 --- a/xen/include/asm-x86/p2m.h +++ b/xen/include/asm-x86/p2m.h @@ -613,6 +613,9 @@ void p2m_memory_type_changed(struct domain *d); int p2m_is_logdirty_range(struct p2m_domain *, unsigned long start, unsigned long end); +/* Set foreign entry in the p2m table (for priv-mapping) */ +int set_foreign_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn); + /* Set mmio addresses in the p2m table (for pass-through) */ int set_mmio_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn, unsigned int order, p2m_access_t access); diff --git a/xen/include/public/memory.h b/xen/include/public/memory.h index 29386df98b..9bf58e7384 100644 --- a/xen/include/public/memory.h +++ b/xen/include/public/memory.h @@ -650,7 +650,43 @@ struct xen_vnuma_topology_info { typedef struct xen_vnuma_topology_info xen_vnuma_topology_info_t; DEFINE_XEN_GUEST_HANDLE(xen_vnuma_topology_info_t); -/* Next available subop number is 28 */ +#if defined(__XEN__) || defined(__XEN_TOOLS__) + +/* + * Get the pages for a particular guest resource, so that they can be + * mapped directly by a tools domain. + */ +#define XENMEM_acquire_resource 28 +struct xen_mem_acquire_resource { + /* IN - the domain whose resource is to be mapped */ + domid_t domid; + /* IN - the type of resource (defined below) */ + uint16_t type; + +#define XENMEM_resource_grant_table 0 + + /* + * IN - a type-specific resource identifier, which must be zero + * unless stated otherwise. + */ + uint32_t id; + /* IN - number of (4K) frames of the resource to be mapped */ + uint32_t nr_frames; + /* IN - the index of the initial frame to be mapped */ + uint64_aligned_t frame; + /* IN/OUT - If the tools domain is PV then, upon return, gmfn_list + * will be populated with the MFNs of the resource. + * If the tools domain is HVM then it is expected that, on + * entry, gmfn_list will be populated with a list of GFNs + * that will be mapped to the MFNs of the resource. + */ + XEN_GUEST_HANDLE(xen_pfn_t) gmfn_list; +}; +typedef struct xen_mem_acquire_resource xen_mem_acquire_resource_t; + +#endif /* defined(__XEN__) || defined(__XEN_TOOLS__) */ + +/* Next available subop number is 29 */ #endif /* __XEN_PUBLIC_MEMORY_H__ */ diff --git a/xen/include/xen/grant_table.h b/xen/include/xen/grant_table.h index 43ec6c4d80..f9e89375bb 100644 --- a/xen/include/xen/grant_table.h +++ b/xen/include/xen/grant_table.h @@ -136,6 +136,7 @@ static inline unsigned int grant_to_status_frames(int grant_frames) int mem_sharing_gref_to_gfn(struct grant_table *gt, grant_ref_t ref, gfn_t *gfn, uint16_t *status); +mfn_t gnttab_get_frame(struct domain *d, unsigned long idx); int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn, mfn_t *mfn);