From patchwork Fri Oct 10 11:43:58 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Stabellini X-Patchwork-Id: 38553 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wg0-f71.google.com (mail-wg0-f71.google.com [74.125.82.71]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 2E33620D3D for ; Fri, 10 Oct 2014 11:47:26 +0000 (UTC) Received: by mail-wg0-f71.google.com with SMTP id y10sf1987163wgg.6 for ; Fri, 10 Oct 2014 04:47:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:in-reply-to :references:mime-version:cc:subject:precedence:list-id :list-unsubscribe:list-post:list-help:list-subscribe:sender :errors-to:x-original-sender:x-original-authentication-results :mailing-list:list-archive:content-type:content-transfer-encoding; bh=n4Wklr/z9huRPBR9HD7WB3ZPIKiZ8YFb2tZKcIQNNLE=; b=R2sV5mCMNU2WxEFZx01NcZl/XI6l5tHDU43vgd9nxxfhGuTHfn+k3X9I8yKj2YQbBx blPfqkrmCa8RjjJ3CjoXnLEkupAnfZvAgOHocSNyHEd63B4IYdOK+CUf4t7cf31MoIzJ J9Q1mD7VJ6aUlwjWZjSpOXrYlNhkzH5juT8cr6X/pAuyDDZskCVLa+4UoIkgot7w/bog NkXUKrNqffTp5yH2eC6Xfs5Rg5paar0ZrLg0fsH815JDcwvqNAf3AKNVPGhd4DlpaLr1 xEbrVABWWQpOrFj+QgnWKfR97gTONedlKpbctBOW79g7IfKbe/wEgGx5kL45dKZSuLbI ISLg== X-Gm-Message-State: ALoCoQktlfXyHjsOK9cYr/00g08LIXATIS+7lSXEy8I4883gOo7rVQOZEcZ5k3QYJytY7mOXWtpc X-Received: by 10.152.2.97 with SMTP id 1mr598532lat.6.1412941644930; Fri, 10 Oct 2014 04:47:24 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.234.233 with SMTP id uh9ls277407lac.70.gmail; Fri, 10 Oct 2014 04:47:24 -0700 (PDT) X-Received: by 10.112.219.3 with SMTP id pk3mr4267007lbc.14.1412941644751; Fri, 10 Oct 2014 04:47:24 -0700 (PDT) Received: from mail-lb0-f182.google.com (mail-lb0-f182.google.com [209.85.217.182]) by mx.google.com with ESMTPS id k3si8828143lbs.64.2014.10.10.04.47.24 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 10 Oct 2014 04:47:24 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.182 as permitted sender) client-ip=209.85.217.182; Received: by mail-lb0-f182.google.com with SMTP id z11so2827339lbi.41 for ; Fri, 10 Oct 2014 04:47:24 -0700 (PDT) X-Received: by 10.112.52.165 with SMTP id u5mr4112061lbo.80.1412941644573; Fri, 10 Oct 2014 04:47:24 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.84.229 with SMTP id c5csp411500lbz; Fri, 10 Oct 2014 04:47:23 -0700 (PDT) X-Received: by 10.229.74.136 with SMTP id u8mr7240223qcj.5.1412941642060; Fri, 10 Oct 2014 04:47:22 -0700 (PDT) Received: from lists.xen.org (lists.xen.org. [50.57.142.19]) by mx.google.com with ESMTPS id 4si10573181qav.89.2014.10.10.04.47.17 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Fri, 10 Oct 2014 04:47:22 -0700 (PDT) Received-SPF: none (google.com: xen-devel-bounces@lists.xen.org does not designate permitted sender hosts) client-ip=50.57.142.19; Received: from localhost ([127.0.0.1] helo=lists.xen.org) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1XcYeT-0007oU-QX; Fri, 10 Oct 2014 11:46:29 +0000 Received: from mail6.bemta4.messagelabs.com ([85.158.143.247]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1XcYeR-0007lS-8P for xen-devel@lists.xensource.com; Fri, 10 Oct 2014 11:46:27 +0000 Received: from [85.158.143.35:58024] by server-1.bemta-4.messagelabs.com id C1/4E-05872-217C7345; Fri, 10 Oct 2014 11:46:26 +0000 X-Env-Sender: Stefano.Stabellini@citrix.com X-Msg-Ref: server-7.tower-21.messagelabs.com!1412941582!12094959!5 X-Originating-IP: [66.165.176.89] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni44OSA9PiAyMDMwMDc=\n X-StarScan-Received: X-StarScan-Version: 6.12.3; banners=-,-,- X-VirusChecked: Checked Received: (qmail 25476 invoked from network); 10 Oct 2014 11:46:25 -0000 Received: from smtp.citrix.com (HELO SMTP.CITRIX.COM) (66.165.176.89) by server-7.tower-21.messagelabs.com with RC4-SHA encrypted SMTP; 10 Oct 2014 11:46:25 -0000 X-IronPort-AV: E=Sophos;i="5.04,691,1406592000"; d="scan'208";a="180112522" Received: from ukmail1.uk.xensource.com (10.80.16.128) by smtprelay.citrix.com (10.13.107.78) with Microsoft SMTP Server id 14.3.181.6; Fri, 10 Oct 2014 07:46:21 -0400 Received: from kaball.uk.xensource.com ([10.80.2.59]) by ukmail1.uk.xensource.com with esmtp (Exim 4.69) (envelope-from ) id 1XcYeG-0008F6-Cj; Fri, 10 Oct 2014 12:46:16 +0100 From: Stefano Stabellini To: Date: Fri, 10 Oct 2014 12:43:58 +0100 Message-ID: <1412941440-5646-5-git-send-email-stefano.stabellini@eu.citrix.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: MIME-Version: 1.0 X-DLP: MIA2 Cc: julien.grall@citrix.com, Ian.Campbell@citrix.com, Stefano Stabellini Subject: [Xen-devel] [PATCH v4 5/7] xen/arm: introduce GNTTABOP_cache_flush X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: List-Unsubscribe: , List-Post: , List-Help: , List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: stefano.stabellini@eu.citrix.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.182 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Archive: Introduce a new hypercall to perform cache maintenance operation on behalf of the guest. The argument is a machine address and a size. The implementation checks that the memory range is owned by the guest or the guest has been granted access to it by another domain. Introduce grant_map_exists: an internal grant table function to check whether an mfn has been granted to a given domain on a target grant table. Signed-off-by: Stefano Stabellini --- Changes in v4: - ASSERT(spin_is_locked); - return instead of break in grant_map_exists; - pass a pointer to __gnttab_cache_flush; - code style; - unsigned int iterator in gnttab_cache_flush; - return ret instead -ret; - cflush.offset >= PAGE_SIZE return -EINVAL. Changes in v3: - reduce the time the grant_table lock is held; - fix warning message; - s/EFAULT/EPERM; - s/llx/PRIx64; - check offset and size independetly before checking their sum; - rcu_lock_current_domain cannot fail; - s/ENOSYS/EOPNOTSUPP; - use clean_and_invalidate_xen_dcache_va_range to do both operations at once; - fold grant_map_exists in this patch; - support "count" argument; - make correspondent changes to compat/grant_table.c; - introduce GNTTAB_CACHE_SOURCE_GREF to select the type of input in the union; - rename size field to length; - make length and offset uint16_t; - only take spin_lock if d != owner. Changes in v2: - do not check for mfn_to_page errors; - take a reference to the page; - replace printk with gdprintk; - split long line; - remove out label; - move rcu_lock_current_domain down before the loop. - move the hypercall to GNTTABOP; - take a spin_lock before calling grant_map_exists. --- xen/common/compat/grant_table.c | 8 +++ xen/common/grant_table.c | 134 ++++++++++++++++++++++++++++++++++++++ xen/include/public/grant_table.h | 20 ++++++ xen/include/xlat.lst | 1 + 4 files changed, 163 insertions(+) diff --git a/xen/common/compat/grant_table.c b/xen/common/compat/grant_table.c index 7ebbbc1..0ffab97 100644 --- a/xen/common/compat/grant_table.c +++ b/xen/common/compat/grant_table.c @@ -51,6 +51,10 @@ CHECK_gnttab_get_version; CHECK_gnttab_swap_grant_ref; #undef xen_gnttab_swap_grant_ref +#define xen_gnttab_cache_flush gnttab_cache_flush +CHECK_gnttab_cache_flush; +#undef xen_gnttab_cache_flush + int compat_grant_table_op(unsigned int cmd, XEN_GUEST_HANDLE_PARAM(void) cmp_uop, unsigned int count) @@ -106,6 +110,10 @@ int compat_grant_table_op(unsigned int cmd, CASE(swap_grant_ref); #endif +#ifndef CHECK_gnttab_cache_flush + CASE(cache_flush); +#endif + #undef CASE default: return do_grant_table_op(cmd, cmp_uop, count); diff --git a/xen/common/grant_table.c b/xen/common/grant_table.c index 5451c75..a989d81 100644 --- a/xen/common/grant_table.c +++ b/xen/common/grant_table.c @@ -483,6 +483,34 @@ static int _set_status(unsigned gt_version, return _set_status_v2(domid, readonly, mapflag, shah, act, status); } +static bool_t grant_map_exists(const struct domain *ld, + struct grant_table *rgt, + unsigned long mfn) +{ + const struct active_grant_entry *act; + grant_ref_t ref; + + ASSERT(spin_is_locked(&rgt->lock)); + + for ( ref = 0; ref != nr_grant_entries(rgt); ref++ ) + { + act = &active_entry(rgt, ref); + + if ( !act->pin ) + continue; + + if ( act->domid != ld->domain_id ) + continue; + + if ( act->frame != mfn ) + continue; + + return 1; + } + + return 0; +} + static void mapcount( struct grant_table *lgt, struct domain *rd, unsigned long mfn, unsigned int *wrc, unsigned int *rdc) @@ -2481,6 +2509,98 @@ gnttab_swap_grant_ref(XEN_GUEST_HANDLE_PARAM(gnttab_swap_grant_ref_t) uop, return 0; } +static int __gnttab_cache_flush(gnttab_cache_flush_t *cflush) +{ + struct domain *d, *owner; + struct page_info *page; + uint64_t mfn; + void *v; + + if ( (cflush->offset >= PAGE_SIZE) || + (cflush->length > PAGE_SIZE) || + (cflush->offset + cflush->length > PAGE_SIZE) ) + return -EINVAL; + + if ( cflush->length == 0 || cflush->op == 0 ) + return 0; + + /* currently unimplemented */ + if ( cflush->op & GNTTAB_CACHE_SOURCE_GREF ) + return -EOPNOTSUPP; + + d = rcu_lock_current_domain(); + mfn = cflush->a.dev_bus_addr >> PAGE_SHIFT; + + if ( !mfn_valid(mfn) ) + { + rcu_unlock_domain(d); + return -EINVAL; + } + + page = mfn_to_page(mfn); + owner = page_get_owner_and_reference(page); + if ( !owner ) + { + rcu_unlock_domain(d); + return -EPERM; + } + + if ( d != owner ) + { + spin_lock(&owner->grant_table->lock); + + if ( !grant_map_exists(d, owner->grant_table, mfn) ) + { + spin_unlock(&owner->grant_table->lock); + rcu_unlock_domain(d); + put_page(page); + gdprintk(XENLOG_G_ERR, "mfn %"PRIx64" hasn't been granted by d%d to d%d\n", + mfn, owner->domain_id, d->domain_id); + return -EINVAL; + } + } + + v = map_domain_page(mfn); + v += cflush->offset; + + if ( (cflush->op & GNTTAB_CACHE_INVAL) && (cflush->op & GNTTAB_CACHE_CLEAN) ) + clean_and_invalidate_dcache_va_range(v, cflush->length); + else if ( cflush->op & GNTTAB_CACHE_INVAL ) + invalidate_dcache_va_range(v, cflush->length); + else if ( cflush->op & GNTTAB_CACHE_CLEAN ) + clean_dcache_va_range(v, cflush->length); + + if ( d != owner ) + spin_unlock(&owner->grant_table->lock); + unmap_domain_page(v); + put_page(page); + + return 0; +} + +static long +gnttab_cache_flush(XEN_GUEST_HANDLE_PARAM(gnttab_cache_flush_t) uop, + unsigned int count) +{ + int ret; + unsigned int i; + gnttab_cache_flush_t op; + + for ( i = 0; i < count; i++ ) + { + if ( i && hypercall_preempt_check() ) + return i; + if ( unlikely(__copy_from_guest(&op, uop, 1)) ) + return -EFAULT; + ret = __gnttab_cache_flush(&op); + if ( ret < 0 ) + return ret; + guest_handle_add_offset(uop, 1); + } + return 0; +} + + long do_grant_table_op( unsigned int cmd, XEN_GUEST_HANDLE_PARAM(void) uop, unsigned int count) @@ -2610,6 +2730,20 @@ do_grant_table_op( } break; } + case GNTTABOP_cache_flush: + { + XEN_GUEST_HANDLE_PARAM(gnttab_cache_flush_t) cflush = + guest_handle_cast(uop, gnttab_cache_flush_t); + if ( unlikely(!guest_handle_okay(cflush, count)) ) + goto out; + rc = gnttab_cache_flush(cflush, count); + if ( rc > 0 ) + { + guest_handle_add_offset(cflush, rc); + uop = guest_handle_cast(cflush, void); + } + break; + } default: rc = -ENOSYS; break; diff --git a/xen/include/public/grant_table.h b/xen/include/public/grant_table.h index b8a3d6c..20d4e77 100644 --- a/xen/include/public/grant_table.h +++ b/xen/include/public/grant_table.h @@ -309,6 +309,7 @@ typedef uint16_t grant_status_t; #define GNTTABOP_get_status_frames 9 #define GNTTABOP_get_version 10 #define GNTTABOP_swap_grant_ref 11 +#define GNTTABOP_cache_flush 12 #endif /* __XEN_INTERFACE_VERSION__ */ /* ` } */ @@ -574,6 +575,25 @@ struct gnttab_swap_grant_ref { typedef struct gnttab_swap_grant_ref gnttab_swap_grant_ref_t; DEFINE_XEN_GUEST_HANDLE(gnttab_swap_grant_ref_t); +/* + * Issue one or more cache maintenance operations on a portion of a + * page granted to the calling domain by a foreign domain. + */ +struct gnttab_cache_flush { + union { + uint64_t dev_bus_addr; + grant_ref_t ref; + } a; + uint16_t offset; /* offset from start of grant */ + uint16_t length; /* size within the grant */ +#define GNTTAB_CACHE_CLEAN (1<<0) +#define GNTTAB_CACHE_INVAL (1<<1) +#define GNTTAB_CACHE_SOURCE_GREF (1<<31) + uint32_t op; +}; +typedef struct gnttab_cache_flush gnttab_cache_flush_t; +DEFINE_XEN_GUEST_HANDLE(gnttab_cache_flush_t); + #endif /* __XEN_INTERFACE_VERSION__ */ /* diff --git a/xen/include/xlat.lst b/xen/include/xlat.lst index 9a35dd7..3822b00 100644 --- a/xen/include/xlat.lst +++ b/xen/include/xlat.lst @@ -51,6 +51,7 @@ ? grant_entry_header grant_table.h ? grant_entry_v2 grant_table.h ? gnttab_swap_grant_ref grant_table.h +? gnttab_cache_flush grant_table.h ? kexec_exec kexec.h ! kexec_image kexec.h ! kexec_range kexec.h