From patchwork Wed Jan 21 13:25:44 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Julien Grall X-Patchwork-Id: 43444 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 81817240D5 for ; Wed, 21 Jan 2015 13:28:41 +0000 (UTC) Received: by mail-wg0-f71.google.com with SMTP id k14sf21321858wgh.2 for ; Wed, 21 Jan 2015 05:28:40 -0800 (PST) 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 :mime-version:content-length:cc:subject:precedence:list-id :list-unsubscribe:list-post:list-help:list-subscribe:content-type :content-transfer-encoding:sender:errors-to:x-original-sender :x-original-authentication-results:mailing-list:list-archive; bh=F7Dpz0YGKYt3qjl99YCtJ6+oXkjpnMLgNs7ytaDSiOE=; b=LE1KQbaQFb4jJiw8pHvCi6xa5HYVK+x1B65cGRAkjIVV/O1frvZ+F/NwMi/E0yFGCg dyCpcGOBmayx5Sc/Ypu+nKjCJAPq9jd24+ocxwq7vFN1KNF7R/4VtlolDrHDQevNZmVf 8HCdyQi49XDTMtKIostmW5gPME42TA+VJyZFu7aGfxAhFIaRMs4MzlqwQHIwpfeafoFk btWrhIfUFB5GAlAbNbsCiGTyKDDUjxe1xAfEq7ay4swnjuDJwEif82Kg6XPlb3exVC6G /oTkkQjmWdGBRFvfHBBdOr3l6ZrnPRLELKHMZUIEXJhF9JYL4zsI7Coc1y7PpfpExVW3 Fevg== X-Gm-Message-State: ALoCoQm9Sv6RQJy222PGbDL8/K/6xfWD7ZUDbHKZi8XnLErnLR2NVUABXsHbtQIH0trq9F/te6Rq X-Received: by 10.180.228.38 with SMTP id sf6mr3574740wic.5.1421846920764; Wed, 21 Jan 2015 05:28:40 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.88.99 with SMTP id bf3ls35939lab.13.gmail; Wed, 21 Jan 2015 05:28:40 -0800 (PST) X-Received: by 10.152.205.75 with SMTP id le11mr43971215lac.20.1421846920534; Wed, 21 Jan 2015 05:28:40 -0800 (PST) Received: from mail-la0-f51.google.com (mail-la0-f51.google.com. [209.85.215.51]) by mx.google.com with ESMTPS id ld3si19272743lac.115.2015.01.21.05.28.40 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 21 Jan 2015 05:28:40 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.51 as permitted sender) client-ip=209.85.215.51; Received: by mail-la0-f51.google.com with SMTP id ge10so19046829lab.10 for ; Wed, 21 Jan 2015 05:28:40 -0800 (PST) X-Received: by 10.112.30.71 with SMTP id q7mr44195040lbh.41.1421846920435; Wed, 21 Jan 2015 05:28:40 -0800 (PST) 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.9.200 with SMTP id c8csp1776365lbb; Wed, 21 Jan 2015 05:28:39 -0800 (PST) X-Received: by 10.224.19.137 with SMTP id a9mr36531624qab.83.1421846918634; Wed, 21 Jan 2015 05:28:38 -0800 (PST) Received: from lists.xen.org (lists.xen.org. [50.57.142.19]) by mx.google.com with ESMTPS id c6si4493029qca.40.2015.01.21.05.28.38 (version=TLSv1 cipher=RC4-SHA bits=128/128); Wed, 21 Jan 2015 05:28:38 -0800 (PST) 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 1YDvIg-000580-Pg; Wed, 21 Jan 2015 13:26:26 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1YDvIf-00057v-2B for xen-devel@lists.xenproject.org; Wed, 21 Jan 2015 13:26:25 +0000 Received: from [85.158.139.211] by server-8.bemta-5.messagelabs.com id 46/98-11581-009AFB45; Wed, 21 Jan 2015 13:26:24 +0000 X-Env-Sender: julien.grall@linaro.org X-Msg-Ref: server-8.tower-206.messagelabs.com!1421846783!15371667!1 X-Originating-IP: [74.125.82.50] X-SpamReason: No, hits=0.5 required=7.0 tests=BODY_RANDOM_LONG X-StarScan-Received: X-StarScan-Version: 6.12.5; banners=-,-,- X-VirusChecked: Checked Received: (qmail 25297 invoked from network); 21 Jan 2015 13:26:23 -0000 Received: from mail-wg0-f50.google.com (HELO mail-wg0-f50.google.com) (74.125.82.50) by server-8.tower-206.messagelabs.com with RC4-SHA encrypted SMTP; 21 Jan 2015 13:26:23 -0000 Received: by mail-wg0-f50.google.com with SMTP id b13so11269900wgh.9 for ; Wed, 21 Jan 2015 05:26:23 -0800 (PST) X-Received: by 10.194.234.2 with SMTP id ua2mr22828476wjc.40.1421846783282; Wed, 21 Jan 2015 05:26:23 -0800 (PST) Received: from chilopoda.uk.xensource.com. ([185.25.64.249]) by mx.google.com with ESMTPSA id j1sm25540571wjw.25.2015.01.21.05.26.21 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 21 Jan 2015 05:26:22 -0800 (PST) From: Julien Grall To: xen-devel@lists.xenproject.org Date: Wed, 21 Jan 2015 13:25:44 +0000 Message-Id: <1421846744-31795-1-git-send-email-julien.grall@linaro.org> X-Mailer: git-send-email 2.1.4 MIME-Version: 1.0 Cc: Wei Liu , ian.campbell@citrix.com, Stefano Stabellini , Andrew Cooper , Julien Grall , tim@xen.org, stefano.stabellini@citrix.com, Jan Beulich , Ian Jackson , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Subject: [Xen-devel] [PATCH v2] libxc: introduce a per architecture scratch pfn for temporary grant mapping 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: julien.grall@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.51 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: The code to initialize the grant table in libxc uses xc_domain_maximum_gpfn() + 1 to get a guest pfn for mapping the grant frame and to initialize it. This solution has two major issues: - The check of the return of xc_domain_maximum_gpfn is buggy because xen_pfn_t is unsigned and in case of an error -ERRNO is returned. Which is never catch with ( pfn <= 0 ). - The guest memory layout maybe filled up to the end, i.e xc_domain_maximum_gpfn() + 1 gives either 0 or an invalid PFN due to hardware limitation. Futhermore, on ARM, xc_domain_maximum_gpfn() is not implemented and return -ENOSYS. This will make libxc to use always the same PFN which may colapse with an already mapped region (see xen/include/public/arch-arm.h for the layout). This patch only address the problem for ARM, the x86 version use the same behavior (ie xc_domain_maximum_gpfn() + 1), as I'm not familiar with Xen x86. A new function xc_core_arch_get_scratch_gpfn is introduced to be able to choose the gpfn per architecture. For the ARM version, we use the GUEST_GNTTAB_GUEST which is the base of the region by the guest to map the grant table. At the build time, nothing is mapped there. At the same time correctly check the return of xc_domain_maximum_gpfn for x86. Signed-off-by: Julien Grall Cc: Jan Beulich Cc: Andrew Cooper Cc: Roger Pau Monné Cc: Ian Jackson Cc: Stefano Stabellini Cc: Ian Campbell Cc: Wei Liu --- This patch is candidate for backport to Xen 4.5. With the support of MMIO passthrough, we may end up to erase an MMIO region. I don't think it's required for Xen 4.4. I chose to take this appproach after the discussion on implementing XENMEM_maximum_gpfn on ARM (https://patches.linaro.org/32894/). This patch has only been built tested on x86 and the same behavior has been kept (i.e xc_domain_maximum_gpfn() + 1). I would be happy if someone for x86 world is looking for a possible solution. Changes in v2: - x86: The +1 was missing to get the scratch pfn - Fix mispelling in comment in xc_core_arch_get_gpfn for ARM --- tools/libxc/xc_core.h | 3 +++ tools/libxc/xc_core_arm.c | 17 +++++++++++++++++ tools/libxc/xc_core_x86.c | 17 +++++++++++++++++ tools/libxc/xc_dom_boot.c | 18 ++++++++++++------ 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/tools/libxc/xc_core.h b/tools/libxc/xc_core.h index 10cbfca..5867030 100644 --- a/tools/libxc/xc_core.h +++ b/tools/libxc/xc_core.h @@ -148,6 +148,9 @@ int xc_core_arch_map_p2m_writable(xc_interface *xch, unsigned int guest_width, shared_info_any_t *live_shinfo, xen_pfn_t **live_p2m, unsigned long *pfnp); +int xc_core_arch_get_scratch_gpfn(xc_interface *xch, domid_t domid, + xen_pfn_t *gpfn); + #if defined (__i386__) || defined (__x86_64__) # include "xc_core_x86.h" diff --git a/tools/libxc/xc_core_arm.c b/tools/libxc/xc_core_arm.c index 2fbcf3f..16508e7 100644 --- a/tools/libxc/xc_core_arm.c +++ b/tools/libxc/xc_core_arm.c @@ -96,6 +96,23 @@ xc_core_arch_map_p2m_writable(xc_interface *xch, unsigned int guest_width, xc_do return xc_core_arch_map_p2m_rw(xch, dinfo, info, live_shinfo, live_p2m, pfnp, 1); } + +int +xc_core_arch_get_scratch_gpfn(xc_interface *xch, domid_t domid, + xen_pfn_t *gpfn) +{ + /* + * The Grant Table region space is not used until the guest is + * booting. Use the first page for the scratch pfn. + */ + XC_BUILD_BUG_ON(GUEST_GNTTAB_SIZE < XC_PAGE_SIZE); + + *gpfn = GUEST_GNTTAB_BASE >> XC_PAGE_SHIFT; + + return 0; +} + + /* * Local variables: * mode: C diff --git a/tools/libxc/xc_core_x86.c b/tools/libxc/xc_core_x86.c index f05060a..fac99ec 100644 --- a/tools/libxc/xc_core_x86.c +++ b/tools/libxc/xc_core_x86.c @@ -205,6 +205,23 @@ xc_core_arch_map_p2m_writable(xc_interface *xch, unsigned int guest_width, xc_do return xc_core_arch_map_p2m_rw(xch, dinfo, info, live_shinfo, live_p2m, pfnp, 1); } + +int +xc_core_arch_get_scratch_gpfn(xc_interface *xch, domid_t domid, + xen_pfn_t *gpfn) +{ + int rc; + + rc = xc_domain_maximum_gpfn(xch, domid); + + if ( rc <= 0 ) + return rc; + + *gpfn = rc + 1; + + return 0; +} + /* * Local variables: * mode: C diff --git a/tools/libxc/xc_dom_boot.c b/tools/libxc/xc_dom_boot.c index f0a1c64..a141eb5 100644 --- a/tools/libxc/xc_dom_boot.c +++ b/tools/libxc/xc_dom_boot.c @@ -33,6 +33,7 @@ #include "xg_private.h" #include "xc_dom.h" +#include "xc_core.h" #include #include @@ -365,7 +366,7 @@ int xc_dom_gnttab_hvm_seed(xc_interface *xch, domid_t domid, domid_t xenstore_domid) { int rc; - xen_pfn_t max_gfn; + xen_pfn_t scratch_gpfn; struct xen_add_to_physmap xatp = { .domid = domid, .space = XENMAPSPACE_grant_table, @@ -375,16 +376,21 @@ int xc_dom_gnttab_hvm_seed(xc_interface *xch, domid_t domid, .domid = domid, }; - max_gfn = xc_domain_maximum_gpfn(xch, domid); - if ( max_gfn <= 0 ) { + rc = xc_core_arch_get_scratch_gpfn(xch, domid, &scratch_gpfn); + if ( rc < 0 ) + { xc_dom_panic(xch, XC_INTERNAL_ERROR, - "%s: failed to get max gfn " + "%s: failed to get a scratch gfn " "[errno=%d]\n", __FUNCTION__, errno); return -1; } - xatp.gpfn = max_gfn + 1; - xrfp.gpfn = max_gfn + 1; + xatp.gpfn = scratch_gpfn; + xrfp.gpfn = scratch_gpfn; + + xc_dom_printf(xch, "%s: called, pfn=0x%"PRI_xen_pfn, __FUNCTION__, + scratch_gpfn); + rc = do_memory_op(xch, XENMEM_add_to_physmap, &xatp, sizeof(xatp)); if ( rc != 0 )