From: Kieran Mansley Subject: RFC: Using grant table to give iomem permission Date: Fri, 18 May 2007 15:01:23 +0100 Message-ID: <1179496883.14376.27.camel@moonstone.uk.level5networks.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-y1pU+avHJzR8CccYzNcE" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: xen-devel@lists.xensource.com List-Id: xen-devel@lists.xenproject.org --=-y1pU+avHJzR8CccYzNcE Content-Type: text/plain Content-Transfer-Encoding: 7bit It used to be possible to use domctl to programatically set up permissions for other domains to map iomem regions. However, domctl is no longer accessible from drivers (only from tools). After some discussion with Keir about how to achieve this functionality, it was suggested that it might be possible to use the grant table operations to do it. Attached is a patch that has a go at this and seems to work. However, it is quite primitive and I would welcome the comments of others who know more about the grant tables than I do! The patch adds a new type of grant (GNTMAP_iomem_map) to complement GNTMAP_device_map and GNTMAP_host_map. The granting domain would do a normal grant operation to specify the region that can be used as iomem: err = gnttab_grant_foreign_access(dev->otherend_id, mfn, 0) and then pass the grant ref to the mapping domain. The mapping domain would then do a map op, along the lines of: struct gnttab_map_grant_ref op; gnttab_set_map_op(&op, 0, GNTMAP_iomem_map, gnt_ref, dev->otherend_id); if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1)) BUG(); When the hypervisor tries to do this grant in xen/common/grant_table.c it (i) notices that it is an iomem_map rather than device_map or host_map; (ii) it is from dom0; and (iii) it is not a RAM page. It then attempts to call iomem_permit_access(). If successful, on return, the mapping domain should then be able to call ioremap() to access the page in question. When finished, the mapping domain can similarly unmap the grant, which removes its ability to ioremap() the page: struct gnttab_unmap_grant_ref op; gnttab_set_unmap_op(&op, 0, GNTMAP_iomem_map, handle); if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1)) BUG(); Some questions from me: - does this approach seem sane? - any problem with adding the GNTTAB_iomem_map type? - how about the current test for it being a RAM page: !mfn_valid (frame). I've seen this return "valid" on iomem pages on a machine with 4GB of RAM, so it may not be a good/sufficient test. - the current patch doesn't actually do the ioremap() as part of the grant operation, it just sets up the permissions. I can see that others might prefer it to do both. Any thoughts? - is there some gross omission from what I've done? Thanks Kieran --=-y1pU+avHJzR8CccYzNcE Content-Disposition: attachment; filename=iomem_grants Content-Type: text/x-patch; name=iomem_grants; charset=UTF-8 Content-Transfer-Encoding: 7bit diff -r 08e2c03a47e0 xen/common/grant_table.c --- a/xen/common/grant_table.c Tue May 15 17:47:46 2007 +0100 +++ b/xen/common/grant_table.c Thu May 17 09:53:29 2007 +0100 @@ -195,7 +195,9 @@ __gnttab_map_grant_ref( led = current; ld = led->domain; - if ( unlikely((op->flags & (GNTMAP_device_map|GNTMAP_host_map)) == 0) ) + if ( unlikely((op->flags & (GNTMAP_device_map | + GNTMAP_host_map | + GNTMAP_iomem_map)) == 0) ) { gdprintk(XENLOG_INFO, "Bad flags in grant map op (%x).\n", op->flags); op->status = GNTST_bad_gntref; @@ -305,16 +307,36 @@ __gnttab_map_grant_ref( spin_unlock(&rd->grant_table->lock); - if ( unlikely(!mfn_valid(frame)) || - unlikely(!((op->flags & GNTMAP_readonly) ? - get_page(mfn_to_page(frame), rd) : - get_page_and_type(mfn_to_page(frame), rd, - PGT_writable_page))) ) - { - if ( !rd->is_dying ) - gdprintk(XENLOG_WARNING, "Could not pin grant frame %lx\n", frame); - rc = GNTST_general_error; - goto undo_out; + if ( op->flags & GNTMAP_iomem_map ) + { + if( op->dom == 0 && !mfn_valid(frame) ) { + /* Could be an iomem page for setting up permission */ + if ( iomem_permit_access(ld, frame, frame) != 0 ) { + gdprintk(XENLOG_WARNING, + "Could not permit access to grant frame %lx as iomem\n", + frame); + rc = GNTST_general_error; + goto undo_out; + } + } else { + gdprintk(XENLOG_WARNING, + "Invalid domain (%d) or frame (%lx) for iomem map\n", + op->dom, frame); + rc = GNTST_general_error; + goto undo_out; + } + } else { + if ( unlikely(!mfn_valid(frame)) || + unlikely(!((op->flags & GNTMAP_readonly) ? + get_page(mfn_to_page(frame), rd) : + get_page_and_type(mfn_to_page(frame), rd, + PGT_writable_page))) ) + { + if ( !rd->is_dying ) + gdprintk(XENLOG_WARNING, "Could not pin grant frame %lx\n", frame); + rc = GNTST_general_error; + goto undo_out; + } } if ( op->flags & GNTMAP_host_map ) @@ -494,8 +516,17 @@ __gnttab_unmap_grant_ref( put_page_and_type(mfn_to_page(frame)); } } - - if ( (map->flags & (GNTMAP_device_map|GNTMAP_host_map)) == 0 ) + + if ( flags & GNTMAP_iomem_map ) + { + map->flags &= ~GNTMAP_iomem_map; + + rc = iomem_deny_access(ld, frame, frame); + } + + if ( (map->flags & (GNTMAP_device_map | + GNTMAP_host_map | + GNTMAP_iomem_map)) == 0 ) { map->flags = 0; put_maptrack_handle(ld->grant_table, op->handle); @@ -1352,13 +1383,16 @@ gnttab_release_mappings( struct domain *rd; struct active_grant_entry *act; struct grant_entry *sha; + int rc; BUG_ON(!d->is_dying); for ( handle = 0; handle < gt->maptrack_limit; handle++ ) { map = &maptrack_entry(gt, handle); - if ( !(map->flags & (GNTMAP_device_map|GNTMAP_host_map)) ) + if ( !(map->flags & (GNTMAP_device_map | + GNTMAP_host_map | + GNTMAP_iomem_map)) ) continue; ref = map->ref; @@ -1415,6 +1449,9 @@ gnttab_release_mappings( if ( (act->pin & (GNTPIN_devw_mask|GNTPIN_hstw_mask)) == 0 ) gnttab_clear_flag(_GTF_writing, &sha->flags); } + + if( map->flags & GNTMAP_iomem_map ) + rc = iomem_deny_access(rd, act->frame, act->frame); if ( act->pin == 0 ) gnttab_clear_flag(_GTF_reading, &sha->flags); diff -r 08e2c03a47e0 xen/include/public/grant_table.h --- a/xen/include/public/grant_table.h Tue May 15 17:47:46 2007 +0100 +++ b/xen/include/public/grant_table.h Thu May 17 09:53:29 2007 +0100 @@ -358,6 +358,12 @@ DEFINE_XEN_GUEST_HANDLE(gnttab_query_siz #define GNTMAP_contains_pte (1<<_GNTMAP_contains_pte) /* + * Establish iomem permission + */ +#define _GNTMAP_iomem_map (5) +#define GNTMAP_iomem_map (1<<_GNTMAP_iomem_map) + +/* * Values for error status returns. All errors are -ve. */ #define GNTST_okay (0) /* Normal return. */ --=-y1pU+avHJzR8CccYzNcE Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --=-y1pU+avHJzR8CccYzNcE--