From patchwork Tue Jan 13 20:10:48 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Julien Grall X-Patchwork-Id: 43086 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wg0-f72.google.com (mail-wg0-f72.google.com [74.125.82.72]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 0C37A20DE8 for ; Tue, 13 Jan 2015 20:13:10 +0000 (UTC) Received: by mail-wg0-f72.google.com with SMTP id a1sf2789706wgh.3 for ; Tue, 13 Jan 2015 12:13:09 -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=KbC0Ri9fU2ot1Dd4K+YRvnawUCQ2Bd/7I4IB13/3ZqI=; b=DrZr9IuU+XVWVL+sHyEwRouvj64eZhmCvwi73uzM0Jf9RIlDSH0PQPReHsrbYP4q2+ Wb2l6KRSDGyxr5+5G/gmV4GSLj7wg9tjf4NOXF5okEppSdL+8kS0+fZeyiB+G31rZp63 bXzW+w3frvOtA+BBct+jS0xUlrwXfXDk4y+v0tVezIpaAL8NpLJXprhCjh3yGHrxXIPd NZ1hTSN1cp0Rqg6BFzf+O50Qsmjprah2eQGq0NgtKT17CPMd3DbgRh8jp+tImUAmnAV3 Jt2mkDvGVNl1pCPfkaeR8F3bakDywl9/iAPn2iKK9d293IqJL+E8Q+qfud2794+G1Vy/ Qkpg== X-Gm-Message-State: ALoCoQkjHXgiLrCFRErzukzF9ZefcckGzIgsvDclGCvjiWZ9mUVrYOiCrKQVKQuIOfUNKZVfQ9yW X-Received: by 10.180.24.35 with SMTP id r3mr156790wif.0.1421179989249; Tue, 13 Jan 2015 12:13:09 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.5.97 with SMTP id r1ls39503lar.81.gmail; Tue, 13 Jan 2015 12:13:09 -0800 (PST) X-Received: by 10.152.3.9 with SMTP id 9mr220752lay.54.1421179989085; Tue, 13 Jan 2015 12:13:09 -0800 (PST) Received: from mail-la0-f49.google.com (mail-la0-f49.google.com. [209.85.215.49]) by mx.google.com with ESMTPS id z4si3843332lae.103.2015.01.13.12.13.09 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 13 Jan 2015 12:13:09 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.49 as permitted sender) client-ip=209.85.215.49; Received: by mail-la0-f49.google.com with SMTP id hs14so4624345lab.8 for ; Tue, 13 Jan 2015 12:13:09 -0800 (PST) X-Received: by 10.152.6.132 with SMTP id b4mr107497laa.59.1421179988923; Tue, 13 Jan 2015 12:13:08 -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 c8csp1488522lbb; Tue, 13 Jan 2015 12:13:08 -0800 (PST) X-Received: by 10.220.71.20 with SMTP id f20mr269890vcj.36.1421179987409; Tue, 13 Jan 2015 12:13:07 -0800 (PST) Received: from lists.xen.org (lists.xen.org. [50.57.142.19]) by mx.google.com with ESMTPS id ek1si1468106vdc.104.2015.01.13.12.13.06 (version=TLSv1 cipher=RC4-SHA bits=128/128); Tue, 13 Jan 2015 12:13:07 -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 1YB7o9-0001iC-Ao; Tue, 13 Jan 2015 20:11:21 +0000 Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1YB7o7-0001i6-Oa for xen-devel@lists.xenproject.org; Tue, 13 Jan 2015 20:11:19 +0000 Received: from [193.109.254.147] by server-3.bemta-14.messagelabs.com id 00/09-02953-7EB75B45; Tue, 13 Jan 2015 20:11:19 +0000 X-Env-Sender: julien.grall@linaro.org X-Msg-Ref: server-13.tower-27.messagelabs.com!1421179878!20383775!1 X-Originating-IP: [74.125.82.44] 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 25088 invoked from network); 13 Jan 2015 20:11:18 -0000 Received: from mail-wg0-f44.google.com (HELO mail-wg0-f44.google.com) (74.125.82.44) by server-13.tower-27.messagelabs.com with RC4-SHA encrypted SMTP; 13 Jan 2015 20:11:18 -0000 Received: by mail-wg0-f44.google.com with SMTP id y19so5100963wgg.3 for ; Tue, 13 Jan 2015 12:11:17 -0800 (PST) X-Received: by 10.194.93.194 with SMTP id cw2mr188821wjb.21.1421179877806; Tue, 13 Jan 2015 12:11:17 -0800 (PST) Received: from chilopoda.uk.xensource.com. ([185.25.64.249]) by mx.google.com with ESMTPSA id i3sm27061990wjw.2.2015.01.13.12.11.16 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 13 Jan 2015 12:11:17 -0800 (PST) From: Julien Grall To: xen-devel@lists.xenproject.org Date: Tue, 13 Jan 2015 20:10:48 +0000 Message-Id: <1421179848-31833-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 , Stefano Stabellini , Andrew Cooper , Julien Grall , Ian Jackson , Jan Beulich , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Subject: [Xen-devel] [PATCH] 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.49 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 --- 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. --- 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..4c34191 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 scrach 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..b157d85 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; + + 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 )