From patchwork Mon Sep 9 13:43:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 11137951 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 D04D714DB for ; Mon, 9 Sep 2019 13:45:36 +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 A1C3A218AF for ; Mon, 9 Sep 2019 13:45:36 +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="PEIamQaS" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A1C3A218AF 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 1i7JxF-000559-3i; Mon, 09 Sep 2019 13:43:41 +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 1i7JxD-000552-Hl for xen-devel@lists.xenproject.org; Mon, 09 Sep 2019 13:43:39 +0000 X-Inumbo-ID: d32f44de-d307-11e9-ac0b-12813bfff9fa Received: from esa3.hc3370-68.iphmx.com (unknown [216.71.145.155]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id d32f44de-d307-11e9-ac0b-12813bfff9fa; Mon, 09 Sep 2019 13:43:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1568036618; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=YAZ4775fBadIlBfI5JqH7rk7PZPNkVMwoLJpEBsCQ6w=; b=PEIamQaSscMXcZUul/LwmgQu1AWEPN9uodJxdirtxmvIF9DtvIvkU/kJ hPnLw9anTQemiB88b6D4hJCwc2pzxBQWVrrfOtTYhf6jJ3UCCxmzbfTzL 3NfJ85EBq155YHaWMpPVYu3ON5g3TrMWQxHsVzssqplpCTqCD2+QmPVVb o=; Authentication-Results: esa3.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 (esa3.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=esa3.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="andrew.cooper3@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa3.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=esa3.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 (esa3.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=esa3.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: tGFVU+aAkkSXmXYTAdfU0WoI5LMYF4awI2DfQa6F2q839fCHkw3eekZqXg2lXMc4HnAI/4VeEr Ae9WTXER4hTi/t1TEChEPElsF3hU6EYakuK2k1RM2kQChY335W0sG2NBkDPuE7U6vqMTucUKfY rOPibIwoHf8fRlqRmyn5whPyDUCwdz6Ojrok7RlMj4f+Y0tV9CUhqKGPQdrddmvfWi5nOL+U7F 2T2cCJdSDJ9LZdD7lQSgagCTynyctFU38bqlr87oL6TvIUGLZybcgXPD0/GuOGMmIswPNCjARW xsw= X-SBRS: 2.7 X-MesageID: 5320671 X-Ironport-Server: esa3.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.64,484,1559534400"; d="scan'208";a="5320671" From: Andrew Cooper To: Xen-devel Date: Mon, 9 Sep 2019 14:43:33 +0100 Message-ID: <20190909134333.10927-1-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 MIME-Version: 1.0 Subject: [Xen-devel] [PATCH v2] tools/ocaml: Fix xenctrl ABI and introduce build-time checks 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: Wei Liu , Andrew Cooper , Rob Hoes , =?utf-8?b?RWR3aW4gVMO2csO2aw==?= , Christian Lindig , Ian Jackson , =?utf-8?q?Roger_Pau_Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" c/s f089fddd941 broke the Ocaml ABI by renumering XEN_SYSCTL_PHYSCAP_directio without adjusting the Ocaml physinfo_cap_flag enumeration. Fix this by inserting CAP_PV between CAP_HVM and CAP_DirectIO. Factor out the bitmap-to-list conversion logic into a helper, to avoid an opencoded truncation of the bitmap. To cover this, add BUILD_BUG_ON()'s at the caller for each constant in the C-to-Ocaml conversion, and for the the applicable max/all constant. This will result in a compile time failure whenever constants get renumbered/added without a compatible adjustment to the Ocaml ABI. Signed-off-by: Andrew Cooper --- CC: Ian Jackson CC: Wei Liu CC: Roger Pau Monné CC: Christian Lindig CC: Rob Hoes CC: Edwin Török This needs backporting to Xen 4.12 v2: * Base this patch correctly. --- tools/ocaml/libs/xc/xenctrl.ml | 1 + tools/ocaml/libs/xc/xenctrl.mli | 5 ++- tools/ocaml/libs/xc/xenctrl_stubs.c | 77 +++++++++++++++++++++++++++---------- xen/include/public/sysctl.h | 4 ++ 4 files changed, 66 insertions(+), 21 deletions(-) diff --git a/tools/ocaml/libs/xc/xenctrl.ml b/tools/ocaml/libs/xc/xenctrl.ml index 35958b94d5..cd7e95a6fa 100644 --- a/tools/ocaml/libs/xc/xenctrl.ml +++ b/tools/ocaml/libs/xc/xenctrl.ml @@ -99,6 +99,7 @@ type sched_control = type physinfo_cap_flag = | CAP_HVM + | CAP_PV | CAP_DirectIO type physinfo = diff --git a/tools/ocaml/libs/xc/xenctrl.mli b/tools/ocaml/libs/xc/xenctrl.mli index 6c4268d453..0bd06ed920 100644 --- a/tools/ocaml/libs/xc/xenctrl.mli +++ b/tools/ocaml/libs/xc/xenctrl.mli @@ -82,7 +82,10 @@ type domaininfo = { arch_config : arch_domainconfig; } type sched_control = { weight : int; cap : int; } -type physinfo_cap_flag = CAP_HVM | CAP_DirectIO +type physinfo_cap_flag = + | CAP_HVM + | CAP_PV + | CAP_DirectIO type physinfo = { threads_per_core : int; cores_per_socket : int; diff --git a/tools/ocaml/libs/xc/xenctrl_stubs.c b/tools/ocaml/libs/xc/xenctrl_stubs.c index 2e1b29ce33..ce8dbba437 100644 --- a/tools/ocaml/libs/xc/xenctrl_stubs.c +++ b/tools/ocaml/libs/xc/xenctrl_stubs.c @@ -32,6 +32,7 @@ #define XC_WANT_COMPAT_MAP_FOREIGN_API #include +#include #include "mmap_stubs.h" @@ -119,6 +120,31 @@ static void domain_handle_of_uuid_string(xen_domain_handle_t h, #undef X } +/* + * Various fields which are a bitmap in the C ABI are converted to lists of + * integers in the Ocaml ABI for more idiomatic handling. + */ +static value c_bitmap_to_ocaml_list(unsigned int bitmap) +{ + CAMLparam0(); + CAMLlocal2(list, tmp); + + list = tmp = Val_emptylist; + + for ( unsigned int i = 0; bitmap; i++, bitmap >>= 1 ) + { + if ( !(bitmap & 1) ) + continue; + + tmp = caml_alloc_small(2, Tag_cons); + Field(tmp, 0) = Val_int(i); + Field(tmp, 1) = list; + list = tmp; + } + + CAMLreturn(list); +} + CAMLprim value stub_xc_domain_create(value xch, value config) { CAMLparam2(xch, config); @@ -315,16 +341,25 @@ static value alloc_domaininfo(xc_domaininfo_t * info) Store_field(result, 15, tmp); #if defined(__i386__) || defined(__x86_64__) - /* emulation_flags: x86_arch_emulation_flags list; */ - tmp = emul_list = Val_emptylist; - for (i = 0; i < 10; i++) { - if ((info->arch_config.emulation_flags >> i) & 1) { - tmp = caml_alloc_small(2, Tag_cons); - Field(tmp, 0) = Val_int(i); - Field(tmp, 1) = emul_list; - emul_list = tmp; - } - } + /* + * emulation_flags: x86_arch_emulation_flags list; + * + * These BUILD_BUG_ON()'s map the C ABI to the Ocaml ABI. If they + * trip, xenctrl.ml{,i} need updating to match. + */ + BUILD_BUG_ON(XEN_X86_EMU_LAPIC != (1u << 0)); + BUILD_BUG_ON(XEN_X86_EMU_HPET != (1u << 1)); + BUILD_BUG_ON(XEN_X86_EMU_PM != (1u << 2)); + BUILD_BUG_ON(XEN_X86_EMU_RTC != (1u << 3)); + BUILD_BUG_ON(XEN_X86_EMU_IOAPIC != (1u << 4)); + BUILD_BUG_ON(XEN_X86_EMU_PIC != (1u << 5)); + BUILD_BUG_ON(XEN_X86_EMU_VGA != (1u << 6)); + BUILD_BUG_ON(XEN_X86_EMU_IOMMU != (1u << 7)); + BUILD_BUG_ON(XEN_X86_EMU_PIT != (1u << 8)); + BUILD_BUG_ON(XEN_X86_EMU_USE_PIRQ != (1u << 9)); + BUILD_BUG_ON(XEN_X86_EMU_VPCI != (1u << 10)); + BUILD_BUG_ON(XEN_X86_EMU_ALL != 0x7ff); + emul_list = c_bitmap_to_ocaml_list(info->arch_config.emulation_flags); /* xen_x86_arch_domainconfig */ x86_arch_config = caml_alloc_tuple(1); @@ -635,7 +670,7 @@ CAMLprim value stub_xc_send_debug_keys(value xch, value keys) CAMLprim value stub_xc_physinfo(value xch) { CAMLparam1(xch); - CAMLlocal3(physinfo, cap_list, tmp); + CAMLlocal2(physinfo, cap_list); xc_physinfo_t c_physinfo; int r; @@ -646,15 +681,17 @@ CAMLprim value stub_xc_physinfo(value xch) if (r) failwith_xc(_H(xch)); - tmp = cap_list = Val_emptylist; - for (r = 0; r < 2; r++) { - if ((c_physinfo.capabilities >> r) & 1) { - tmp = caml_alloc_small(2, Tag_cons); - Field(tmp, 0) = Val_int(r); - Field(tmp, 1) = cap_list; - cap_list = tmp; - } - } + /* + * capabilities: physinfo_cap_flag list; + * + * These BUILD_BUG_ON()'s map the C ABI to the Ocaml ABI. If they + * trip, xenctrl.ml{,i} need updating to match. + */ + BUILD_BUG_ON(XEN_SYSCTL_PHYSCAP_hvm != (1u << 0)); + BUILD_BUG_ON(XEN_SYSCTL_PHYSCAP_pv != (1u << 1)); + BUILD_BUG_ON(XEN_SYSCTL_PHYSCAP_directio != (1u << 2)); + BUILD_BUG_ON(XEN_SYSCTL_PHYSCAP_MAX != XEN_SYSCTL_PHYSCAP_directio); + cap_list = c_bitmap_to_ocaml_list(c_physinfo.capabilities); physinfo = caml_alloc_tuple(10); Store_field(physinfo, 0, Val_int(c_physinfo.threads_per_core)); diff --git a/xen/include/public/sysctl.h b/xen/include/public/sysctl.h index 36b3f8c429..5401f9c2fe 100644 --- a/xen/include/public/sysctl.h +++ b/xen/include/public/sysctl.h @@ -90,6 +90,10 @@ struct xen_sysctl_tbuf_op { /* The platform supports direct access to I/O devices with IOMMU. */ #define _XEN_SYSCTL_PHYSCAP_directio 2 #define XEN_SYSCTL_PHYSCAP_directio (1u<<_XEN_SYSCTL_PHYSCAP_directio) + +/* Max XEN_SYSCTL_PHYSCAP_* constant. Used for ABI checking. */ +#define XEN_SYSCTL_PHYSCAP_MAX XEN_SYSCTL_PHYSCAP_directio + struct xen_sysctl_physinfo { uint32_t threads_per_core; uint32_t cores_per_socket;