From patchwork Wed Sep 11 20:05:01 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 11141839 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5DA4A17E6 for ; Wed, 11 Sep 2019 20:07:05 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 37C802085B for ; Wed, 11 Sep 2019 20:07:05 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=citrix.com header.i=@citrix.com header.b="Fb6x6l7G" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 37C802085B Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=citrix.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1i88rf-000301-GT; Wed, 11 Sep 2019 20:05:19 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1i88re-0002zv-4q for xen-devel@lists.xenproject.org; Wed, 11 Sep 2019 20:05:18 +0000 X-Inumbo-ID: 76dcce3b-d4cf-11e9-83dd-12813bfff9fa Received: from esa2.hc3370-68.iphmx.com (unknown [216.71.145.153]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 76dcce3b-d4cf-11e9-83dd-12813bfff9fa; Wed, 11 Sep 2019 20:05:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1568232314; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Vf1AG29drMqw2LT/Z66r4aJJqf5+EsSaB0/oRaGhy1U=; b=Fb6x6l7GhTtLYwIetbfFRQlzkbgUyg6bqmtagtEDAUifZ/XM6EbUURgv UiLUDy5iGMPw1rvtvLxPukM15ApHR3IXdeIJyelWrGwdFF6mtvKF7k9WP Ka5Q7Q89WRiCS2PIfZUebHb41Yn1omUlRID0Su2LvPpJvynrG2fhYSrmD M=; Authentication-Results: esa2.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none; spf=None smtp.pra=andrew.cooper3@citrix.com; spf=Pass smtp.mailfrom=Andrew.Cooper3@citrix.com; spf=None smtp.helo=postmaster@mail.citrix.com Received-SPF: None (esa2.hc3370-68.iphmx.com: no sender authenticity information available from domain of andrew.cooper3@citrix.com) identity=pra; client-ip=162.221.158.21; receiver=esa2.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="andrew.cooper3@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa2.hc3370-68.iphmx.com: domain of Andrew.Cooper3@citrix.com designates 162.221.158.21 as permitted sender) identity=mailfrom; client-ip=162.221.158.21; receiver=esa2.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="Andrew.Cooper3@citrix.com"; x-conformance=sidf_compatible; x-record-type="v=spf1"; x-record-text="v=spf1 ip4:209.167.231.154 ip4:178.63.86.133 ip4:195.66.111.40/30 ip4:85.115.9.32/28 ip4:199.102.83.4 ip4:192.28.146.160 ip4:192.28.146.107 ip4:216.52.6.88 ip4:216.52.6.188 ip4:162.221.158.21 ip4:162.221.156.83 ~all" Received-SPF: None (esa2.hc3370-68.iphmx.com: no sender authenticity information available from domain of postmaster@mail.citrix.com) identity=helo; client-ip=162.221.158.21; receiver=esa2.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: +nKyXPXKqy3lx81FteDEfZSU9DIo1LYIMo+nWZao9CWra4UpLmTBCdgMMtCiWqVUE0P0VfMQ9P Fwr7JQz2ywdM8lDwSJIs/4YM6nh8Cex8eJ8wv+1wZT1xEAMoEQeBM6TKQ9hoRadptEJYGeZtHU exFt891zTg7MDwErmVyJNoiJHeVhCtTOyBM8qZLOsObF+GLaWrN93Y9146dqCQX9sZEwCVU5Ti pzZ/83z1EkFCk1jCJSa1PCGIHrLObPrv9XqCjKtwfGl+a9/baIvSOOFfI+UhMhohuqdy7j19OU 1q4= X-SBRS: 2.7 X-MesageID: 5451849 X-Ironport-Server: esa2.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.64,494,1559534400"; d="scan'208";a="5451849" From: Andrew Cooper To: Xen-devel Date: Wed, 11 Sep 2019 21:05:01 +0100 Message-ID: <20190911200504.5693-6-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190911200504.5693-1-andrew.cooper3@citrix.com> References: <20190911200504.5693-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Subject: [Xen-devel] [PATCH 5/8] tools/libxc: Rework xc_cpuid_set() to use {get, set}_cpu_policy() X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Andrew Cooper , Ian Jackson , Wei Liu , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" The purpose of this change is to stop using xc_cpuid_do_domctl(), and to stop basing decisions on a local CPUID instruction. This is not an appropriate way to construct policy information for other domains. Obtain the host and domain-max policies from Xen, and mix the results as before. Provide rather more error logging than before. Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich --- CC: Jan Beulich CC: Wei Liu CC: Roger Pau Monné CC: Ian Jackson --- tools/libxc/xc_cpuid_x86.c | 95 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 84 insertions(+), 11 deletions(-) diff --git a/tools/libxc/xc_cpuid_x86.c b/tools/libxc/xc_cpuid_x86.c index a2d29a0fae..d1a2b61214 100644 --- a/tools/libxc/xc_cpuid_x86.c +++ b/tools/libxc/xc_cpuid_x86.c @@ -905,20 +905,80 @@ int xc_cpuid_set( const char **config, char **config_transformed) { int rc; - unsigned int i, j, regs[4], polregs[4]; - struct cpuid_domain_info info = {}; + unsigned int i, j, regs[4] = {}, polregs[4] = {}; + xc_dominfo_t di; + xen_cpuid_leaf_t *leaves = NULL; + unsigned int nr_leaves, policy_leaves, nr_msrs; + uint32_t err_leaf = -1, err_subleaf = -1, err_msr = -1; for ( i = 0; i < 4; ++i ) config_transformed[i] = NULL; - rc = get_cpuid_domain_info(xch, domid, &info, NULL, 0); + if ( xc_domain_getinfo(xch, domid, 1, &di) != 1 || + di.domid != domid ) + { + ERROR("Failed to obtain d%d info", domid); + rc = -ESRCH; + goto fail; + } + + rc = xc_get_cpu_policy_size(xch, &nr_leaves, &nr_msrs); if ( rc ) - goto out; + { + PERROR("Failed to obtain policy info size"); + rc = -errno; + goto fail; + } - cpuid(input, regs); + rc = -ENOMEM; + if ( (leaves = calloc(nr_leaves, sizeof(*leaves))) == NULL ) + { + ERROR("Unable to allocate memory for %u CPUID leaves", nr_leaves); + goto fail; + } - memcpy(polregs, regs, sizeof(regs)); - xc_cpuid_policy(&info, input, polregs); + /* Get the domain's max policy. */ + nr_msrs = 0; + policy_leaves = nr_leaves; + rc = xc_get_system_cpu_policy(xch, di.hvm ? XEN_SYSCTL_cpu_policy_hvm_max + : XEN_SYSCTL_cpu_policy_pv_max, + &policy_leaves, leaves, &nr_msrs, NULL); + if ( rc ) + { + PERROR("Failed to obtain %s max policy", di.hvm ? "hvm" : "pv"); + rc = -errno; + goto fail; + } + for ( i = 0; i < policy_leaves; ++i ) + if ( leaves[i].leaf == input[0] && leaves[i].subleaf == input[1] ) + { + polregs[0] = leaves[i].a; + polregs[1] = leaves[i].b; + polregs[2] = leaves[i].c; + polregs[3] = leaves[i].d; + break; + } + + /* Get the host policy. */ + nr_msrs = 0; + policy_leaves = nr_leaves; + rc = xc_get_system_cpu_policy(xch, XEN_SYSCTL_cpu_policy_host, + &policy_leaves, leaves, &nr_msrs, NULL); + if ( rc ) + { + PERROR("Failed to obtain host policy"); + rc = -errno; + goto fail; + } + for ( i = 0; i < policy_leaves; ++i ) + if ( leaves[i].leaf == input[0] && leaves[i].subleaf == input[1] ) + { + regs[0] = leaves[i].a; + regs[1] = leaves[i].b; + regs[2] = leaves[i].c; + regs[3] = leaves[i].d; + break; + } for ( i = 0; i < 4; i++ ) { @@ -969,9 +1029,21 @@ int xc_cpuid_set( } } - rc = xc_cpuid_do_domctl(xch, domid, input, regs); - if ( rc == 0 ) - goto out; + /* Feed the transformed leaf back up to Xen. */ + leaves[0] = (xen_cpuid_leaf_t){ input[0], input[1], + regs[0], regs[1], regs[2], regs[3] }; + rc = xc_set_domain_cpu_policy(xch, domid, 1, leaves, 0, NULL, + &err_leaf, &err_subleaf, &err_msr); + if ( rc ) + { + PERROR("Failed to set d%d's policy (err leaf %#x, subleaf %#x, msr %#x)", + domid, err_leaf, err_subleaf, err_msr); + rc = -errno; + goto fail; + } + + /* Success! */ + goto out; fail: for ( i = 0; i < 4; i++ ) @@ -981,6 +1053,7 @@ int xc_cpuid_set( } out: - free_cpuid_domain_info(&info); + free(leaves); + return rc; }