From patchwork Tue Jan 31 21:29:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 13123467 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 06466C636CC for ; Tue, 31 Jan 2023 21:29:47 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.487844.755557 (Exim 4.92) (envelope-from ) id 1pMyC4-0000Nz-6D; Tue, 31 Jan 2023 21:29:32 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 487844.755557; Tue, 31 Jan 2023 21:29:32 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pMyC4-0000Ns-23; Tue, 31 Jan 2023 21:29:32 +0000 Received: by outflank-mailman (input) for mailman id 487844; Tue, 31 Jan 2023 21:29:30 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pMyC2-0000Nb-CI for xen-devel@lists.xenproject.org; Tue, 31 Jan 2023 21:29:30 +0000 Received: from esa1.hc3370-68.iphmx.com (esa1.hc3370-68.iphmx.com [216.71.145.142]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 54e58ec1-a1ae-11ed-933c-83870f6b2ba8; Tue, 31 Jan 2023 22:29:28 +0100 (CET) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 54e58ec1-a1ae-11ed-933c-83870f6b2ba8 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1675200568; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=jNU72Uu2tmrcqHFCNgIV8t/XfjcVgtHdUCV/DbhunZ0=; b=QMTAIaJm5uUkfYsHhHh2hOmT50uppvvn2vGnDBTiyGu9DAV8jDMUCRHI deD4VWRrMcJ8iF4eZBFHj1uUyUTGWch1DkfbvylTBb428J9UtTjolI0s9 +3zUzq1u89Eb8QCyeAhb3jWz95EGclaGS6TXrvLTyzEadp/LYwBXR092n 4=; Authentication-Results: esa1.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none X-SBRS: 4.0 X-MesageID: 95499176 X-Ironport-Server: esa1.hc3370-68.iphmx.com X-Remote-IP: 162.221.156.123 X-Policy: $RELAYED IronPort-Data: A9a23:DRPIu6rBBa1a1sQTvyEoYRPaRKNeBmIZZRIvgKrLsJaIsI4StFCzt garIBmGOayJYmr3L4onPYrg9k1XucfSz4BjHQZtrC9kQy0WoJuZCYyVIHmrMnLJJKUvbq7FA +Y2MYCccZ9uHhcwgj/3b9ANeFEljfngqoLUUbKCYWYpAFc+E0/NsDo788YhmIlknNOlNA2Ev NL2sqX3NUSsnjV5KQr40YrawP9UlKm06WxwUmAWP6gR5weHzCBNV/rzGInqR5fGatgMdgKFb 76rIIGRpgvx4xorA9W5pbf3GmVirmn6ZFXmZtJ+AsBOszAazsAA+v9T2Mk0MC+7vw6hjdFpo OihgLTrIesf0g8gr8xGO/VQO3kW0aSrY9YrK1Dn2SCY5xWun3cBX5yCpaz5VGEV0r8fPI1Ay RAXABkVS0ixwMCo+auyR+VI3ZgKHvv6BapK7xmMzRmBZRonaZXKQqGM7t5ExjYgwMtJGJ4yZ eJAN2ApNk6ZJUQSZBFOUslWcOSA3xETdxVxrl6PqLVxyG/U1AFri5DmMcbPe8zMTs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RjwUC3Y7FuKIO5nrZvv3+9161wXh4S3bpXUd WGyvusocq+P2nqK0wx9VMfuuBEFk5DYytuBHJy9/B9mgIm4ExR3g8WwtcSkWwH8494Q55Y5/ 7cOqAtj71WSNQKBJgNcNvpbPHHeFAleyi8RV66MBDiDuUKKnjNo5n47PE84/yrYoUByN83lI 7aWF1VuGYucwa2YPf+qqFj41TIWiGwTD7twsZR69xwvaD9XqPiNWmGREo1m8Wtrv0DConQWu q1OphRH/j/RFGebrphzkn7Qd1fOHMeWMoatpIyXE+PuNvCLsnwuunSYJ/oVcnQ+PYfKxPC61 c4LUnOzZ97nz+Ws1fD8WbsZ08= X-IronPort-AV: E=Sophos;i="5.97,261,1669093200"; d="scan'208";a="95499176" From: Andrew Cooper To: Xen-devel CC: =?utf-8?b?RWR3aW4gVMO2csO2aw==?= , Andrew Cooper , Christian Lindig , David Scott , Rob Hoes Subject: [PATCH 1/7] tools/ocaml/libs: Don't declare stubs as taking void Date: Tue, 31 Jan 2023 21:29:07 +0000 Message-ID: <20230131212913.6199-2-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20230131212913.6199-1-andrew.cooper3@citrix.com> References: <20230131212913.6199-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 From: Edwin Török There is no such thing as an Ocaml function (C stub or otherwise) taking no parameters. In the absence of any other parameters, unit is still passed. This doesn't explode with any ABI we care about, but would malfunction for an ABI environment such as stdcall. Fixes: c3afd398ba7f ("ocaml: Add XS bindings.") Fixes: 8b7ce06a2d34 ("ocaml: Add XC bindings.") Signed-off-by: Edwin Török Signed-off-by: Andrew Cooper --- CC: Christian Lindig CC: David Scott CC: Edwin Török CC: Rob Hoes --- tools/ocaml/libs/xb/xenbus_stubs.c | 5 ++--- tools/ocaml/libs/xc/xenctrl_stubs.c | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/tools/ocaml/libs/xb/xenbus_stubs.c b/tools/ocaml/libs/xb/xenbus_stubs.c index 3065181a55e6..97116b07826a 100644 --- a/tools/ocaml/libs/xb/xenbus_stubs.c +++ b/tools/ocaml/libs/xb/xenbus_stubs.c @@ -30,10 +30,9 @@ #include #include -CAMLprim value stub_header_size(void) +CAMLprim value stub_header_size(value unit) { - CAMLparam0(); - CAMLreturn(Val_int(sizeof(struct xsd_sockmsg))); + return Val_int(sizeof(struct xsd_sockmsg)); } CAMLprim value stub_header_of_string(value s) diff --git a/tools/ocaml/libs/xc/xenctrl_stubs.c b/tools/ocaml/libs/xc/xenctrl_stubs.c index 2fba9c5e94d6..728818445975 100644 --- a/tools/ocaml/libs/xc/xenctrl_stubs.c +++ b/tools/ocaml/libs/xc/xenctrl_stubs.c @@ -86,9 +86,9 @@ static void Noreturn failwith_xc(xc_interface *xch) caml_raise_with_string(*caml_named_value("xc.error"), error_str); } -CAMLprim value stub_xc_interface_open(void) +CAMLprim value stub_xc_interface_open(value unit) { - CAMLparam0(); + CAMLparam1(unit); CAMLlocal1(result); xc_interface *xch; From patchwork Tue Jan 31 21:29:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 13123472 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 48EF5C63797 for ; Tue, 31 Jan 2023 21:29:49 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.487847.755590 (Exim 4.92) (envelope-from ) id 1pMyC6-00015d-35; Tue, 31 Jan 2023 21:29:34 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 487847.755590; Tue, 31 Jan 2023 21:29:34 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pMyC5-000159-Ty; Tue, 31 Jan 2023 21:29:33 +0000 Received: by outflank-mailman (input) for mailman id 487847; Tue, 31 Jan 2023 21:29:32 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pMyC4-0000Nb-Kh for xen-devel@lists.xenproject.org; Tue, 31 Jan 2023 21:29:32 +0000 Received: from esa1.hc3370-68.iphmx.com (esa1.hc3370-68.iphmx.com [216.71.145.142]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 58eef30c-a1ae-11ed-933c-83870f6b2ba8; Tue, 31 Jan 2023 22:29:31 +0100 (CET) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 58eef30c-a1ae-11ed-933c-83870f6b2ba8 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1675200570; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=h6tenPYxyZVuTbfcgU9nfxQU9sqSsuEBVjFHdCiT8ok=; b=ZqQqvOdX5KKbbBc3TH9V8JrKxTvwFWztDm1k7EmLlFMal2Qc2wD+TqMZ MZj+V/IaN4scW7KbBhxfC/5ayssWolx4KVrtyEtccb5tDt0lvvDBvtvEV N8h6tAFJtwyY9c0q7TKebPETQ/bQV7g9QRc9QqhlA2PoLcfTTsmXI2xWG U=; Authentication-Results: esa1.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none X-SBRS: 4.0 X-MesageID: 95499179 X-Ironport-Server: esa1.hc3370-68.iphmx.com X-Remote-IP: 162.221.156.123 X-Policy: $RELAYED IronPort-Data: A9a23:dbqxyaKz/vJ8W1Y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aV/L4a+H+avI0l5Y9e5/F/rU9UNc3h9/gsQw kyAZw== IronPort-HdrOrdr: A9a23:x/ZWIKsggLX9ZL7yRf0m/2E77skCb4Aji2hC6mlwRA09TyXGra 2TdaUgvyMc1gx7ZJh5o6H5BEGBKUm9yXcH2/hrAV7CZniuhILGFvAH0WKP+VPd8mjFh5dgPM RbAuBD4b/LfD9HZK/BiWHVfOrIguP3lpxA7t2urEuFODsaDp2ImD0JaDpzfHcWeCB2Qb4CUL aM7MtOoDStPVwRc8SAH3EAG8TTutHRk5riQBgeQzoq8hOHgz+E4KPzV0Hw5GZVbxp/hZMZtU TVmQ3w4auu99m91x/nzmfWq7hGhdf7zdNHJcqUzuwYMC/lhAqEbJloH5eCoDc2iuey70tCqq iGnz4Qe+BIr1/BdGC8phXgnyP61iw11nPkwViExVP+vM3QXlsBeoZ8rLMcViGcx1srvdl63q 4O9XmerYBrARTJmzm4z8TUVittilG/rRMZ4KEuZj1kIMUjgY1q3MwiFXBuYdQ99eXBmcIa+d xVfYDhDTBtABanhj7izy1SKZeXLw4O91+9MzU/U4quonVrdTlCvjcl7d1akXEa+J0nTZ5Yo+ zCL6RzjblLCtQbdKRnGY46ML+K40H2MGDx2VipUCHaPbBCP2iIp4/84b0z6u3vcJsUzIEqkJ CEVF9Dr2Y9d0/nFMXLhfRwg2bwaXT4WS6oxtBV5pB/tLG5TL33MTebQFRrl8e7uf0QDsDSRv 72MpNLBP3oK3foBO9yrnrDcogXLWNbXNweu949VV7LqsXXKpfyvuiea/rXLKqFK0dWZoo+OA pyYNHeHrQw0qnwYA6GvPH4YQKSRnDC X-IronPort-AV: E=Sophos;i="5.97,261,1669093200"; d="scan'208";a="95499179" From: Andrew Cooper To: Xen-devel CC: Andrew Cooper , Christian Lindig , David Scott , =?utf-8?b?RWR3aW4gVMO2csO2aw==?= , Rob Hoes Subject: [PATCH 2/7] tools/ocaml/libs: Allocate the correct amount of memory for Abstract_tag Date: Tue, 31 Jan 2023 21:29:08 +0000 Message-ID: <20230131212913.6199-3-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20230131212913.6199-1-andrew.cooper3@citrix.com> References: <20230131212913.6199-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 caml_alloc() takes units of Wsize (word size), not bytes. As a consequence, we're allocating 4 or 8 times too much memory. Ocaml has a helper, Wsize_bsize(), but it truncates cases which aren't an exact multiple. Use a BUILD_BUG_ON() to cover the potential for truncation, as there's no rounding-up form of the helper. Fixes: 8b7ce06a2d34 ("ocaml: Add XC bindings.") Fixes: d3e649277a13 ("ocaml: add mmap bindings implementation.") Signed-off-by: Andrew Cooper --- CC: Christian Lindig CC: David Scott CC: Edwin Török CC: Rob Hoes --- tools/ocaml/libs/mmap/Makefile | 2 ++ tools/ocaml/libs/mmap/xenmmap_stubs.c | 6 +++++- tools/ocaml/libs/xc/xenctrl_stubs.c | 5 ++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/tools/ocaml/libs/mmap/Makefile b/tools/ocaml/libs/mmap/Makefile index a62153713571..855b8b2c9877 100644 --- a/tools/ocaml/libs/mmap/Makefile +++ b/tools/ocaml/libs/mmap/Makefile @@ -2,6 +2,8 @@ OCAML_TOPLEVEL=$(CURDIR)/../.. XEN_ROOT=$(OCAML_TOPLEVEL)/../.. include $(OCAML_TOPLEVEL)/common.make +CFLAGS += $(CFLAGS_xeninclude) + OBJS = xenmmap INTF = $(foreach obj, $(OBJS),$(obj).cmi) LIBS = xenmmap.cma xenmmap.cmxa diff --git a/tools/ocaml/libs/mmap/xenmmap_stubs.c b/tools/ocaml/libs/mmap/xenmmap_stubs.c index e03951d781bb..d623ad390e40 100644 --- a/tools/ocaml/libs/mmap/xenmmap_stubs.c +++ b/tools/ocaml/libs/mmap/xenmmap_stubs.c @@ -21,6 +21,8 @@ #include #include "mmap_stubs.h" +#include + #include #include #include @@ -59,7 +61,9 @@ CAMLprim value stub_mmap_init(value fd, value pflag, value mflag, default: caml_invalid_argument("maptype"); } - result = caml_alloc(sizeof(struct mmap_interface), Abstract_tag); + BUILD_BUG_ON((sizeof(struct mmap_interface) % sizeof(value)) != 0); + result = caml_alloc(Wsize_bsize(sizeof(struct mmap_interface)), + Abstract_tag); if (mmap_interface_init(Intf_val(result), Int_val(fd), c_pflag, c_mflag, diff --git a/tools/ocaml/libs/xc/xenctrl_stubs.c b/tools/ocaml/libs/xc/xenctrl_stubs.c index 728818445975..fd1f306f0202 100644 --- a/tools/ocaml/libs/xc/xenctrl_stubs.c +++ b/tools/ocaml/libs/xc/xenctrl_stubs.c @@ -1031,7 +1031,10 @@ CAMLprim value stub_map_foreign_range(value xch, value dom, uint32_t c_dom; unsigned long c_mfn; - result = caml_alloc(sizeof(struct mmap_interface), Abstract_tag); + BUILD_BUG_ON((sizeof(struct mmap_interface) % sizeof(value)) != 0); + result = caml_alloc(Wsize_bsize(sizeof(struct mmap_interface)), + Abstract_tag); + intf = (struct mmap_interface *) result; intf->len = Int_val(size); From patchwork Tue Jan 31 21:29:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 13123473 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 02EBBC38142 for ; Tue, 31 Jan 2023 21:29:47 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.487845.755562 (Exim 4.92) (envelope-from ) id 1pMyC4-0000QS-E2; Tue, 31 Jan 2023 21:29:32 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 487845.755562; Tue, 31 Jan 2023 21:29:32 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pMyC4-0000Pl-9d; Tue, 31 Jan 2023 21:29:32 +0000 Received: by outflank-mailman (input) for mailman id 487845; Tue, 31 Jan 2023 21:29:30 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pMyC2-0000Nb-NH for xen-devel@lists.xenproject.org; Tue, 31 Jan 2023 21:29:30 +0000 Received: from esa2.hc3370-68.iphmx.com (esa2.hc3370-68.iphmx.com [216.71.145.153]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 55eb48d0-a1ae-11ed-933c-83870f6b2ba8; Tue, 31 Jan 2023 22:29:28 +0100 (CET) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 55eb48d0-a1ae-11ed-933c-83870f6b2ba8 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1675200568; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Epp3wFTJjIGliIjbWr7uyY/q1naoD6A5ExyqnrFK5rI=; b=UJUr2HlDbZcKqqNgMq7dcLaPSyT+kXj+eeYjDK03mMc1fylKIMJ91E1y TaCNb/sumgXKP+nVlPEe2xdRk53pUKRw01M9gA7M6QMpBLI+LyZCY6qQC eKpIF+bQGFDtYfCUrJFlVkh6B2hfxOLNL7ya53W/Kst+FxGF+K/Fs+6yL M=; Authentication-Results: esa2.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none X-SBRS: 4.0 X-MesageID: 95024398 X-Ironport-Server: esa2.hc3370-68.iphmx.com X-Remote-IP: 162.221.156.123 X-Policy: $RELAYED IronPort-Data: A9a23:V4+TD6+/ZY02RbRFWKniDrUDg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ys+6S6tIere2JbJY3oMNeJqZtJRmCvt/09ejvgl p1lZnJl9Q== IronPort-HdrOrdr: A9a23:9jBw2K20Bzub96R4VOQEwAqjBEgkLtp133Aq2lEZdPU0SKGlfg 6V/MjztCWE7Ar5PUtLpTnuAsa9qB/nm6KdgrNhWItKPjOW21dARbsKheffKlXbcBEWndQtt5 uIHZIeNDXxZ2IK8PoT4mODYqodKA/sytHWuQ/cpU0dMz2Dc8tbnmBE4p7wKDwMeOFBb6BJcq a01458iBeLX28YVci/DmltZZm4mzWa/KiWGCLvHnQcmXGzsQ8= X-IronPort-AV: E=Sophos;i="5.97,261,1669093200"; d="scan'208";a="95024398" From: Andrew Cooper To: Xen-devel CC: =?utf-8?b?RWR3aW4gVMO2csO2aw==?= , Andrew Cooper , Christian Lindig , David Scott , Rob Hoes Subject: [PATCH 3/7] tools/ocaml/evtchn: Don't reference Custom objects with the GC lock released Date: Tue, 31 Jan 2023 21:29:09 +0000 Message-ID: <20230131212913.6199-4-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20230131212913.6199-1-andrew.cooper3@citrix.com> References: <20230131212913.6199-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 From: Edwin Török The modification to the _H() macro for Ocaml 5 support introduced a subtle bug. From the manual: https://ocaml.org/manual/intfc.html#ss:parallel-execution-long-running-c-code "After caml_release_runtime_system() was called and until caml_acquire_runtime_system() is called, the C code must not access any OCaml data, nor call any function of the run-time system, nor call back into OCaml code." Previously, the value was a naked C pointer, so dereferencing it wasn't "accessing any Ocaml data", but the fix to avoid naked C pointers added a layer of indirection through an Ocaml Custom object, meaning that the common pattern of using _H() in a blocking section is unsafe. In order to fix: * Drop the _H() macro and replace it with a static inline xce_of_val(). * Opencode the assignment into Data_custom_val() in the two constructors. * Rename "value xce" parameters to "value xce_val" so we can consistently have "xenevtchn_handle *xce" on the stack, and obtain the pointer with the GC lock still held. Fixes: 22d5affdf0ce ("tools/ocaml/evtchn: OCaml 5 support, fix potential resource leak") Signed-off-by: Edwin Török Signed-off-by: Andrew Cooper --- CC: Christian Lindig CC: David Scott CC: Edwin Török CC: Rob Hoes --- tools/ocaml/libs/eventchn/xeneventchn_stubs.c | 60 ++++++++++++++++----------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/tools/ocaml/libs/eventchn/xeneventchn_stubs.c b/tools/ocaml/libs/eventchn/xeneventchn_stubs.c index aa8a69cc1ecb..d7881ca95f98 100644 --- a/tools/ocaml/libs/eventchn/xeneventchn_stubs.c +++ b/tools/ocaml/libs/eventchn/xeneventchn_stubs.c @@ -33,11 +33,14 @@ #include #include -#define _H(__h) (*((xenevtchn_handle **)Data_custom_val(__h))) +static inline xenevtchn_handle *xce_of_val(value v) +{ + return *(xenevtchn_handle **)Data_custom_val(v); +} static void stub_evtchn_finalize(value v) { - xenevtchn_close(_H(v)); + xenevtchn_close(xce_of_val(v)); } static struct custom_operations xenevtchn_ops = { @@ -68,7 +71,7 @@ CAMLprim value stub_eventchn_init(value cloexec) caml_failwith("open failed"); result = caml_alloc_custom(&xenevtchn_ops, sizeof(xce), 0, 1); - _H(result) = xce; + *(xenevtchn_handle **)Data_custom_val(result) = xce; CAMLreturn(result); } @@ -87,18 +90,19 @@ CAMLprim value stub_eventchn_fdopen(value fdval) caml_failwith("evtchn fdopen failed"); result = caml_alloc_custom(&xenevtchn_ops, sizeof(xce), 0, 1); - _H(result) = xce; + *(xenevtchn_handle **)Data_custom_val(result) = xce; CAMLreturn(result); } -CAMLprim value stub_eventchn_fd(value xce) +CAMLprim value stub_eventchn_fd(value xce_val) { - CAMLparam1(xce); + CAMLparam1(xce_val); CAMLlocal1(result); + xenevtchn_handle *xce = xce_of_val(xce_val); int fd; - fd = xenevtchn_fd(_H(xce)); + fd = xenevtchn_fd(xce); if (fd == -1) caml_failwith("evtchn fd failed"); @@ -107,13 +111,14 @@ CAMLprim value stub_eventchn_fd(value xce) CAMLreturn(result); } -CAMLprim value stub_eventchn_notify(value xce, value port) +CAMLprim value stub_eventchn_notify(value xce_val, value port) { - CAMLparam2(xce, port); + CAMLparam2(xce_val, port); + xenevtchn_handle *xce = xce_of_val(xce_val); int rc; caml_enter_blocking_section(); - rc = xenevtchn_notify(_H(xce), Int_val(port)); + rc = xenevtchn_notify(xce, Int_val(port)); caml_leave_blocking_section(); if (rc == -1) @@ -122,15 +127,16 @@ CAMLprim value stub_eventchn_notify(value xce, value port) CAMLreturn(Val_unit); } -CAMLprim value stub_eventchn_bind_interdomain(value xce, value domid, +CAMLprim value stub_eventchn_bind_interdomain(value xce_val, value domid, value remote_port) { - CAMLparam3(xce, domid, remote_port); + CAMLparam3(xce_val, domid, remote_port); CAMLlocal1(port); + xenevtchn_handle *xce = xce_of_val(xce_val); xenevtchn_port_or_error_t rc; caml_enter_blocking_section(); - rc = xenevtchn_bind_interdomain(_H(xce), Int_val(domid), Int_val(remote_port)); + rc = xenevtchn_bind_interdomain(xce, Int_val(domid), Int_val(remote_port)); caml_leave_blocking_section(); if (rc == -1) @@ -140,14 +146,15 @@ CAMLprim value stub_eventchn_bind_interdomain(value xce, value domid, CAMLreturn(port); } -CAMLprim value stub_eventchn_bind_virq(value xce, value virq_type) +CAMLprim value stub_eventchn_bind_virq(value xce_val, value virq_type) { - CAMLparam2(xce, virq_type); + CAMLparam2(xce_val, virq_type); CAMLlocal1(port); + xenevtchn_handle *xce = xce_of_val(xce_val); xenevtchn_port_or_error_t rc; caml_enter_blocking_section(); - rc = xenevtchn_bind_virq(_H(xce), Int_val(virq_type)); + rc = xenevtchn_bind_virq(xce, Int_val(virq_type)); caml_leave_blocking_section(); if (rc == -1) @@ -157,13 +164,14 @@ CAMLprim value stub_eventchn_bind_virq(value xce, value virq_type) CAMLreturn(port); } -CAMLprim value stub_eventchn_unbind(value xce, value port) +CAMLprim value stub_eventchn_unbind(value xce_val, value port) { - CAMLparam2(xce, port); + CAMLparam2(xce_val, port); + xenevtchn_handle *xce = xce_of_val(xce_val); int rc; caml_enter_blocking_section(); - rc = xenevtchn_unbind(_H(xce), Int_val(port)); + rc = xenevtchn_unbind(xce, Int_val(port)); caml_leave_blocking_section(); if (rc == -1) @@ -172,14 +180,15 @@ CAMLprim value stub_eventchn_unbind(value xce, value port) CAMLreturn(Val_unit); } -CAMLprim value stub_eventchn_pending(value xce) +CAMLprim value stub_eventchn_pending(value xce_val) { - CAMLparam1(xce); + CAMLparam1(xce_val); CAMLlocal1(result); + xenevtchn_handle *xce = xce_of_val(xce_val); xenevtchn_port_or_error_t port; caml_enter_blocking_section(); - port = xenevtchn_pending(_H(xce)); + port = xenevtchn_pending(xce); caml_leave_blocking_section(); if (port == -1) @@ -189,16 +198,17 @@ CAMLprim value stub_eventchn_pending(value xce) CAMLreturn(result); } -CAMLprim value stub_eventchn_unmask(value xce, value _port) +CAMLprim value stub_eventchn_unmask(value xce_val, value _port) { - CAMLparam2(xce, _port); + CAMLparam2(xce_val, _port); + xenevtchn_handle *xce = xce_of_val(xce_val); evtchn_port_t port; int rc; port = Int_val(_port); caml_enter_blocking_section(); - rc = xenevtchn_unmask(_H(xce), port); + rc = xenevtchn_unmask(xce, port); caml_leave_blocking_section(); if (rc) From patchwork Tue Jan 31 21:29:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 13123471 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 30E51C636D7 for ; Tue, 31 Jan 2023 21:29:48 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.487849.755604 (Exim 4.92) (envelope-from ) id 1pMyC7-0001QU-Q9; Tue, 31 Jan 2023 21:29:35 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 487849.755604; Tue, 31 Jan 2023 21:29:35 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pMyC7-0001PN-LU; Tue, 31 Jan 2023 21:29:35 +0000 Received: by outflank-mailman (input) for mailman id 487849; Tue, 31 Jan 2023 21:29:33 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pMyC5-0000Nm-Kb for xen-devel@lists.xenproject.org; Tue, 31 Jan 2023 21:29:33 +0000 Received: from esa1.hc3370-68.iphmx.com (esa1.hc3370-68.iphmx.com [216.71.145.142]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 58685249-a1ae-11ed-b63b-5f92e7d2e73a; Tue, 31 Jan 2023 22:29:31 +0100 (CET) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 58685249-a1ae-11ed-b63b-5f92e7d2e73a DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1675200571; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=D6fomBGSmJOQ8kYSoARFYZfJ7J0cbqAWh1IAdPyYhsE=; b=JYEzherDJoTOePts40n4Aw3VyqbG07cKdW3/nIiXhQDQl4rtz2h72RNi PL75zCAHkKhjEVHCL41hn6+/1ve1Xfp2v5iOkk7UP2tD4wymOE25Z025/ vQcXVrnvZa+PdpAgj8BOfli07zt620i73jBCXIWNkrrA5xeZQaVIQjE/c k=; Authentication-Results: esa1.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none X-SBRS: 4.0 X-MesageID: 95499181 X-Ironport-Server: esa1.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7zVC1HWMyLPRK9RvsH0K9D+8zN7Tq7eHwP9Dz+ieD2TCfMGd843K6mMrhRAFWs/F+Er L6y9qKil31ibQEJSnKOrdBIcg1WdyhT6FKfg5U/S9Nv6zFOQAkJY8I9C5t4E2C5t8y5Ttv1w 0w= IronPort-HdrOrdr: A9a23:6YTmlaFIAcvJGvc6pLqF8ZLXdLJyesId70hD6qkvc3Fom52j/f xGws5x6fatskdoZJkh8erhBEDyewKmyXcV2/hZAV7MZniDhILFFu9fBM7ZskTd8k7Fh6ZgPM VbAs9D4bTLZDAX4voSojPIderIq+P3k5xA8N2uqkuFOjsaCZ2IgT0ZNi+rVmlNACVWD5swE5 SRouJBujqbYHwSKuirG3UfWODHhtvT0LbrewQPCRIL4BSHyWrA0s+xLzGomjMlFx9fy7Yr9m bI1yT/+6WYqvm+jjPMymPJ6JxSud35jv9OHtaFhMQ5IijlziyoeINicbufuy1dmpDl1H8a1P 335zswNcV67H3cOkuvpwH25gXm2DEyr1f/1F6xmxLY0IDEbQN/L/AEqZNScxPf5UZllsp7yr h302WQsIcSJQ/cnR76+8PDW3hR5wWJSDsZ4KAuZk5kIMsjgYxq3M8iFYRuYdU99RfBmcEa+S 9VfYThDbhtABenhjvizxNSKZSXLwkO91G9MwU/U4WuokRrtWE8wE0CyMMFmHAcsJo7Vplf/u zBdr9ljbdUU6YtHNZA7co6MLmK41b2MGfxGXPXJU6iGLAMOnrLpZKy6LIp5PuycJhNyJcpgp zOXF5RqGZ3IivVeLuz9YwO9gqITHS2XDzrxM0b759luqfkTL6uNSGYUlghn8apvv1aCMzGXP S4Po5QHpbYXBzTMJcM2xe7V4hZKHEYXsFQstEnW0iWqsaOMYHuvvyzSoehGFMsK0dVZorSOA pzYNGoHrQ+0qmCYA6HvCTs X-IronPort-AV: E=Sophos;i="5.97,261,1669093200"; d="scan'208";a="95499181" From: Andrew Cooper To: Xen-devel CC: Andrew Cooper , Christian Lindig , David Scott , =?utf-8?b?RWR3aW4gVMO2csO2aw==?= , Rob Hoes Subject: [PATCH 4/7] tools/ocaml/evtchn: Misc cleanup Date: Tue, 31 Jan 2023 21:29:10 +0000 Message-ID: <20230131212913.6199-5-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20230131212913.6199-1-andrew.cooper3@citrix.com> References: <20230131212913.6199-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 * Remove local integers when all we're returning is Val_int() of another variable. The CAMLlocal*() can't be optimised automatically, as it's registered with the GC. * Rename "virq_type" to "virq" and "_port" to "port". * In stub_eventchn_pending(), rename 'port' to 'rc', to be consistent with all other stubs that return xenevtchn_port_or_error_t. * In stub_eventchn_unmask(), check for rc == -1 to be consistent with all other stubs. No practical change. Signed-off-by: Andrew Cooper --- CC: Christian Lindig CC: David Scott CC: Edwin Török CC: Rob Hoes --- tools/ocaml/libs/eventchn/xeneventchn_stubs.c | 45 +++++++++++---------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/tools/ocaml/libs/eventchn/xeneventchn_stubs.c b/tools/ocaml/libs/eventchn/xeneventchn_stubs.c index d7881ca95f98..34dcfed30275 100644 --- a/tools/ocaml/libs/eventchn/xeneventchn_stubs.c +++ b/tools/ocaml/libs/eventchn/xeneventchn_stubs.c @@ -98,17 +98,15 @@ CAMLprim value stub_eventchn_fdopen(value fdval) CAMLprim value stub_eventchn_fd(value xce_val) { CAMLparam1(xce_val); - CAMLlocal1(result); xenevtchn_handle *xce = xce_of_val(xce_val); int fd; + /* Don't drop the GC lock. This is a simple read out of memory */ fd = xenevtchn_fd(xce); if (fd == -1) caml_failwith("evtchn fd failed"); - result = Val_int(fd); - - CAMLreturn(result); + CAMLreturn(Val_int(fd)); } CAMLprim value stub_eventchn_notify(value xce_val, value port) @@ -131,37 +129,34 @@ CAMLprim value stub_eventchn_bind_interdomain(value xce_val, value domid, value remote_port) { CAMLparam3(xce_val, domid, remote_port); - CAMLlocal1(port); xenevtchn_handle *xce = xce_of_val(xce_val); xenevtchn_port_or_error_t rc; caml_enter_blocking_section(); - rc = xenevtchn_bind_interdomain(xce, Int_val(domid), Int_val(remote_port)); + rc = xenevtchn_bind_interdomain(xce, Int_val(domid), + Int_val(remote_port)); caml_leave_blocking_section(); if (rc == -1) caml_failwith("evtchn bind_interdomain failed"); - port = Val_int(rc); - CAMLreturn(port); + CAMLreturn(Val_int(rc)); } -CAMLprim value stub_eventchn_bind_virq(value xce_val, value virq_type) +CAMLprim value stub_eventchn_bind_virq(value xce_val, value virq) { - CAMLparam2(xce_val, virq_type); - CAMLlocal1(port); + CAMLparam2(xce_val, virq); xenevtchn_handle *xce = xce_of_val(xce_val); xenevtchn_port_or_error_t rc; caml_enter_blocking_section(); - rc = xenevtchn_bind_virq(xce, Int_val(virq_type)); + rc = xenevtchn_bind_virq(xce, Int_val(virq)); caml_leave_blocking_section(); if (rc == -1) caml_failwith("evtchn bind_virq failed"); - port = Val_int(rc); - CAMLreturn(port); + CAMLreturn(Val_int(rc)); } CAMLprim value stub_eventchn_unbind(value xce_val, value port) @@ -183,35 +178,31 @@ CAMLprim value stub_eventchn_unbind(value xce_val, value port) CAMLprim value stub_eventchn_pending(value xce_val) { CAMLparam1(xce_val); - CAMLlocal1(result); xenevtchn_handle *xce = xce_of_val(xce_val); - xenevtchn_port_or_error_t port; + xenevtchn_port_or_error_t rc; caml_enter_blocking_section(); - port = xenevtchn_pending(xce); + rc = xenevtchn_pending(xce); caml_leave_blocking_section(); - if (port == -1) + if (rc == -1) caml_failwith("evtchn pending failed"); - result = Val_int(port); - CAMLreturn(result); + CAMLreturn(Val_int(rc)); } -CAMLprim value stub_eventchn_unmask(value xce_val, value _port) +CAMLprim value stub_eventchn_unmask(value xce_val, value port) { - CAMLparam2(xce_val, _port); + CAMLparam2(xce_val, port); xenevtchn_handle *xce = xce_of_val(xce_val); - evtchn_port_t port; int rc; - port = Int_val(_port); - caml_enter_blocking_section(); - rc = xenevtchn_unmask(xce, port); + rc = xenevtchn_unmask(xce, Int_val(port)); caml_leave_blocking_section(); - if (rc) + if (rc == -1) caml_failwith("evtchn unmask failed"); + CAMLreturn(Val_unit); } From patchwork Tue Jan 31 21:29:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 13123468 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 02EF3C636D4 for ; Tue, 31 Jan 2023 21:29:47 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.487850.755613 (Exim 4.92) (envelope-from ) id 1pMyC8-0001Xy-CO; Tue, 31 Jan 2023 21:29:36 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 487850.755613; Tue, 31 Jan 2023 21:29:36 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pMyC8-0001VA-1g; Tue, 31 Jan 2023 21:29:36 +0000 Received: by outflank-mailman (input) for mailman id 487850; Tue, 31 Jan 2023 21:29:33 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pMyC5-0000Nb-Kl for xen-devel@lists.xenproject.org; Tue, 31 Jan 2023 21:29:33 +0000 Received: from esa1.hc3370-68.iphmx.com (esa1.hc3370-68.iphmx.com [216.71.145.142]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 599ac36b-a1ae-11ed-933c-83870f6b2ba8; Tue, 31 Jan 2023 22:29:32 +0100 (CET) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 599ac36b-a1ae-11ed-933c-83870f6b2ba8 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1675200571; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=YZc3Qe4tQu/04xfxAwG/P7vQhM7bvhY61z2fO5lZ86M=; b=FotiXju3+4liJe4vHjhgOMTCs9TVfB+zVgxBhtX9ltBoKjhWldW57DsF jgDX8ZTyCF3y2Py+klAHfyo7CvrW1UwBAUlmPKhiKZ/dBWo/swI09+eW7 gXS/5P7YIe+u3+XgvypS2cJw+GZJt1RIcTD3loiooBiPJ42kKPMbIxbeE c=; Authentication-Results: esa1.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none X-SBRS: 4.0 X-MesageID: 95499183 X-Ironport-Server: esa1.hc3370-68.iphmx.com X-Remote-IP: 162.221.156.123 X-Policy: $RELAYED IronPort-Data: A9a23:rHDd5a7tMly7k5beTkGK4QxRtDnHchMFZxGqfqrLsTDasY5as4F+v mEcD23TOqzfMWenftsnPozjpEkP6MLXztRrQQNs+CEzHi5G8cbLO4+Ufxz6V8+wwm8vb2o8t plDNYOQRCwQZiWBzvt4GuG59RGQ7YnRGvynTraBYnoqLeNdYH9JoQp5nOIkiZJfj9G8Agec0 fv/uMSaM1K+s9JOGjt8B5mr9VU+45wehBtC5gZlPakQ5QeF/5UoJMl3yZ+ZfiOQrrZ8RoZWd 86bpJml82XQ+QsaC9/Nut4XpWVTH9Y+lSDX4pZnc/DKbipq/0Te4Y5iXBYoUm9Fii3hojxE4 I4lWapc6+seFvakdOw1C3G0GszlVEFM0OevzXOX6aR/w6BaGpdFLjoH4EweZOUlFuhL7W5m3 OQ9cAJKTA66nuu7zJbgFsBetNUaBZy+VG8fkikIITDxCP8nRdbIQrnQ5M8e1zA17ixMNa+AP YxDM2MpN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hR2XiCipZRBRTc2Azg+tnzUQ1wnEbcmwtsrQdTtQ0QbWItUs4QkWUtxjIXLH7GJlO51GkTKp guMCgb3ocSTbrVVQGcgoAl+q3XYp16JGb6fqFFgL3Z79EepgEE82IIgMMYhXsO75Q7Vt1N4P nFKL1hkPVUQtYRdr8VPpZ0fSKbMB2+ffv3ChPmHX33UKUcf37doZ/+57s4oOmsZZwT1ZM33J DMSklRu2I+c1/nTZTm5uw8zjndBGGmGTj9wMBX4JZ0/rX6WbrwKCWGDFQjidGprfkTCtDSH/ yzJJVVCfn+KnaGI/c/4yTuH51JbXUOWswcvdg2H1qIv8LQM4Xv8vfWdf7CTYCdYgrMmlmPck frcAKDVfmotHrbJUMQqCKhJU/QRg== X-IronPort-AV: E=Sophos;i="5.97,261,1669093200"; d="scan'208";a="95499183" From: Andrew Cooper To: Xen-devel CC: =?utf-8?b?RWR3aW4gVMO2csO2aw==?= , Andrew Cooper , Christian Lindig , David Scott , Rob Hoes Subject: [PATCH 5/7] tools/ocaml/xc: Fix binding for xc_domain_assign_device() Date: Tue, 31 Jan 2023 21:29:11 +0000 Message-ID: <20230131212913.6199-6-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20230131212913.6199-1-andrew.cooper3@citrix.com> References: <20230131212913.6199-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 From: Edwin Török The patch adding this binding was plain broken, and unreviewed. It modified the C stub to add a 4th parameter without an equivalent adjustment in the Ocaml side of the bindings. In 64bit builds, this causes us to dereference whatever dead value is in %rcx when trying to interpret the rflags parameter. This has gone unnoticed because Xapi doesn't use this binding (it has its own), but unbreak the binding by passing RDM_RELAXED unconditionally for now (matching the libxl default behaviour). Fixes: 9b34056cb4 ("tools: extend xc_assign_device() to support rdm reservation policy") Signed-off-by: Edwin Török Signed-off-by: Andrew Cooper --- CC: Christian Lindig CC: David Scott CC: Edwin Török CC: Rob Hoes --- tools/ocaml/libs/xc/xenctrl_stubs.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/tools/ocaml/libs/xc/xenctrl_stubs.c b/tools/ocaml/libs/xc/xenctrl_stubs.c index fd1f306f0202..291663bb278a 100644 --- a/tools/ocaml/libs/xc/xenctrl_stubs.c +++ b/tools/ocaml/libs/xc/xenctrl_stubs.c @@ -1245,17 +1245,12 @@ CAMLprim value stub_xc_domain_test_assign_device(value xch, value domid, value d CAMLreturn(Val_bool(ret == 0)); } -static int domain_assign_device_rdm_flag_table[] = { - XEN_DOMCTL_DEV_RDM_RELAXED, -}; - -CAMLprim value stub_xc_domain_assign_device(value xch, value domid, value desc, - value rflag) +CAMLprim value stub_xc_domain_assign_device(value xch, value domid, value desc) { - CAMLparam4(xch, domid, desc, rflag); + CAMLparam3(xch, domid, desc); int ret; int domain, bus, dev, func; - uint32_t sbdf, flag; + uint32_t sbdf; domain = Int_val(Field(desc, 0)); bus = Int_val(Field(desc, 1)); @@ -1263,10 +1258,8 @@ CAMLprim value stub_xc_domain_assign_device(value xch, value domid, value desc, func = Int_val(Field(desc, 3)); sbdf = encode_sbdf(domain, bus, dev, func); - ret = Int_val(Field(rflag, 0)); - flag = domain_assign_device_rdm_flag_table[ret]; - - ret = xc_assign_device(_H(xch), _D(domid), sbdf, flag); + ret = xc_assign_device(_H(xch), _D(domid), sbdf, + XEN_DOMCTL_DEV_RDM_RELAXED); if (ret < 0) failwith_xc(_H(xch)); From patchwork Tue Jan 31 21:29:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 13123469 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 8DF38C6379F for ; Tue, 31 Jan 2023 21:29:48 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.487851.755623 (Exim 4.92) (envelope-from ) id 1pMyC9-0001lE-2r; Tue, 31 Jan 2023 21:29:37 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 487851.755623; Tue, 31 Jan 2023 21:29:37 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pMyC8-0001j6-PF; Tue, 31 Jan 2023 21:29:36 +0000 Received: by outflank-mailman (input) for mailman id 487851; Tue, 31 Jan 2023 21:29:34 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pMyC6-0000Nb-Km for xen-devel@lists.xenproject.org; Tue, 31 Jan 2023 21:29:34 +0000 Received: from esa1.hc3370-68.iphmx.com (esa1.hc3370-68.iphmx.com [216.71.145.142]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 5908c139-a1ae-11ed-933c-83870f6b2ba8; Tue, 31 Jan 2023 22:29:32 +0100 (CET) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 5908c139-a1ae-11ed-933c-83870f6b2ba8 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1675200572; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=DuMXyrr37uOetL3gBS1wPn+orSXwgbl8VJbcI3sGhvc=; b=Ualea35HX4y91C5TstnZbZvNo7xjd/wVtHCYkgPb/79wLexkIhn8FGS5 8R4yewTlbnn0PswKqrQ/g7D0IHilaEizMxQ6vDJIjf3ws1B5d4bAblvpB wiH7KwSZcNP7JRuBueqnDJ+RSW7lczhNEY1DcYk9YJYgjhYTlQBZOoFgT Y=; Authentication-Results: esa1.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none X-SBRS: 4.0 X-MesageID: 95499184 X-Ironport-Server: esa1.hc3370-68.iphmx.com X-Remote-IP: 162.221.156.123 X-Policy: $RELAYED IronPort-Data: A9a23:Gk3+MazYHRxfxxsTyDp6t+c2xirEfRIJ4+MujC+fZmUNrF6WrkUGz 2MYWjuBPKmIYTH8L4onOYvjo0gE75SDydNhSAc5rSAxQypGp/SeCIXCJC8cHc8wwu7rFxs7s ppEOrEsCOhuExcwcz/0auCJQUFUjP3OHfykTbaeYUidfCc8IA85kxVvhuUltYBhhNm9Emult Mj75sbSIzdJ4RYtWo4vw//F+UwHUMja4mtC5QRnPqkT5zcyqlFOZH4hDfDpR5fHatE88t6SH 47r0Ly/92XFyBYhYvvNfmHTKxBirhb6ZGBiu1IOM0SQqkEqSh8ai87XAME0e0ZP4whlqvgqo Dl7WT5cfi9yVkHEsLx1vxC1iEiSN4UekFPMCSDXXcB+UyQq2pYjqhljJBheAGEWxgp4KUhW0 KQqbxITVwmKiu6Sm+6SeNt9v+12eaEHPKtH0p1h5TTQDPJgSpHfWaTao9Rf2V/chOgXQ6yYP ZBAL2MyMlKZOUYn1lQ/UfrSmM+BgHXlfiIeg1WSvactuEDYzRBr0airO93QEjCPbZQIwhfJ/ zKal4j/Ki8lNMbA6AurzmKDueXSkg/ZXZwxEYTto5aGh3XMnzdOWXX6T2CTsfS/z0KzRd9bA 0gV4TY167g/8lSxSdvwVAH+p2SL1jYQUsRdO/c34waMzuzT+QnxO4QfZmcfMpp87pZwHGF0k AbTxLsFGACDrpW8UVfFxPC2swqrMCUZCTReTB02XDIstoyLTJ4IsjrDSdNqEaiQh9LzGC3tz z3ikBXSl4n/nuZQifzloAmvbyaE48GQE1Vrvlm/sneNtFsRWWKzW2C/BbE3B95kJZ3RcFSOt WNsdyO2vLFXVsHleMBgrY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tRTIVbR9BxqQKwDFaTDqKg3QRnA= X-IronPort-AV: E=Sophos;i="5.97,261,1669093200"; d="scan'208";a="95499184" From: Andrew Cooper To: Xen-devel CC: Andrew Cooper , Christian Lindig , David Scott , =?utf-8?b?RWR3aW4gVMO2csO2aw==?= , Rob Hoes Subject: [PATCH 6/7] tools/ocaml/xc: Don't reference Abstract_Tag objects with the GC lock released Date: Tue, 31 Jan 2023 21:29:12 +0000 Message-ID: <20230131212913.6199-7-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20230131212913.6199-1-andrew.cooper3@citrix.com> References: <20230131212913.6199-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 The intf->{addr,len} references in the xc_map_foreign_range() call are unsafe. From the manual: https://ocaml.org/manual/intfc.html#ss:parallel-execution-long-running-c-code "After caml_release_runtime_system() was called and until caml_acquire_runtime_system() is called, the C code must not access any OCaml data, nor call any function of the run-time system, nor call back into OCaml code." More than what the manual says, the intf pointer is (potentially) invalidated by caml_enter_blocking_section() if another thread happens to perform garbage collection at just the right (wrong) moment. Rewrite the logic. There's no need to stash data in the Ocaml object until the success path at the very end. Fixes: 8b7ce06a2d34 ("ocaml: Add XC bindings.") Signed-off-by: Andrew Cooper --- CC: Christian Lindig CC: David Scott CC: Edwin Török CC: Rob Hoes Note: the mmap stub has a similar pattern when constructing a mmap_interface, but, but it's not actually unsafe because it doesn't drop the GC lock. _H() is buggy too, but this patch needs backporting further than that fix. --- tools/ocaml/libs/xc/xenctrl_stubs.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/tools/ocaml/libs/xc/xenctrl_stubs.c b/tools/ocaml/libs/xc/xenctrl_stubs.c index 291663bb278a..e5277f6f19a2 100644 --- a/tools/ocaml/libs/xc/xenctrl_stubs.c +++ b/tools/ocaml/libs/xc/xenctrl_stubs.c @@ -1028,26 +1028,25 @@ CAMLprim value stub_map_foreign_range(value xch, value dom, CAMLparam4(xch, dom, size, mfn); CAMLlocal1(result); struct mmap_interface *intf; - uint32_t c_dom; - unsigned long c_mfn; + unsigned long c_mfn = Nativeint_val(mfn); + int len = Int_val(size); + void *ptr; BUILD_BUG_ON((sizeof(struct mmap_interface) % sizeof(value)) != 0); result = caml_alloc(Wsize_bsize(sizeof(struct mmap_interface)), Abstract_tag); - intf = (struct mmap_interface *) result; - - intf->len = Int_val(size); - - c_dom = _D(dom); - c_mfn = Nativeint_val(mfn); caml_enter_blocking_section(); - intf->addr = xc_map_foreign_range(_H(xch), c_dom, - intf->len, PROT_READ|PROT_WRITE, - c_mfn); + ptr = xc_map_foreign_range(_H(xch), _D(dom), len, + PROT_READ|PROT_WRITE, c_mfn); caml_leave_blocking_section(); - if (!intf->addr) + + if (!ptr) caml_failwith("xc_map_foreign_range error"); + + intf = Data_abstract_val(result); + *intf = (struct mmap_interface){ ptr, len }; + CAMLreturn(result); } From patchwork Tue Jan 31 21:29:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 13123474 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 49EC4C64EC5 for ; Tue, 31 Jan 2023 21:29:50 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.487848.755601 (Exim 4.92) (envelope-from ) id 1pMyC7-0001NM-EG; Tue, 31 Jan 2023 21:29:35 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 487848.755601; Tue, 31 Jan 2023 21:29:35 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pMyC7-0001NA-9y; Tue, 31 Jan 2023 21:29:35 +0000 Received: by outflank-mailman (input) for mailman id 487848; Tue, 31 Jan 2023 21:29:33 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pMyC4-0000Nm-Ku for xen-devel@lists.xenproject.org; Tue, 31 Jan 2023 21:29:33 +0000 Received: from esa3.hc3370-68.iphmx.com (esa3.hc3370-68.iphmx.com [216.71.145.155]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 5645ffb5-a1ae-11ed-b63b-5f92e7d2e73a; Tue, 31 Jan 2023 22:29:27 +0100 (CET) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 5645ffb5-a1ae-11ed-b63b-5f92e7d2e73a DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1675200567; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=tJ4tTGgnhHKyde+wF8qU4LPiC92eyP3hCl2+Pked3dE=; b=h0RIaL/CLB2RtKjhN19reXDuj+bnJZI4CkqtOSzxdvot/NfOX9gbYNeq LHjrL/ZM75xHvyaBL7giR6WQk9kQC8YRhZ2bELP3EHGJVE6KsZ7dzjKEM OhR+Rffut5fpnwEilSmol6HdB32rbB36TXdH0E+Z7d3jG1brKmKKcd9a1 s=; Authentication-Results: esa3.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none X-SBRS: 4.0 X-MesageID: 95097287 X-Ironport-Server: esa3.hc3370-68.iphmx.com X-Remote-IP: 162.221.156.123 X-Policy: $RELAYED IronPort-Data: A9a23:rLtUSq9AKfsAhmGvWTVPDrUDgX6TJUtcMsCJ2f8bNWPcYEJGY0x3x jAZXG3TbP2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nJl9Q== IronPort-HdrOrdr: A9a23:Mzq3j6hycse6p0DbZoBFeMqRMnBQXuUji2hC6mlwRA09TyVXrb HWoB17726NtN91YhsdcL+7Scy9qB/nhPxICMwqTNSftWrd2VdATrsSibcKqgeIc0bDH6xmtZ uIGJIOb+EYY2IK6/oSIzPVLz/j+rS6GWyT6ts2Bk0CcT1X X-IronPort-AV: E=Sophos;i="5.97,261,1669093200"; d="scan'208";a="95097287" From: Andrew Cooper To: Xen-devel CC: =?utf-8?b?RWR3aW4gVMO2csO2aw==?= , "Christian Lindig" , David Scott , Rob Hoes Subject: [PATCH 7/7] tools/ocaml/xc: Don't reference Custom objects with the GC lock released Date: Tue, 31 Jan 2023 21:29:13 +0000 Message-ID: <20230131212913.6199-8-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20230131212913.6199-1-andrew.cooper3@citrix.com> References: <20230131212913.6199-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 From: Edwin Török The modification to the _H() macro for Ocaml 5 support introduced a subtle bug. From the manual: https://ocaml.org/manual/intfc.html#ss:parallel-execution-long-running-c-code "After caml_release_runtime_system() was called and until caml_acquire_runtime_system() is called, the C code must not access any OCaml data, nor call any function of the run-time system, nor call back into OCaml code." Previously, the value was a naked C pointer, so dereferencing it wasn't "accessing any Ocaml data", but the fix to avoid naked C pointers added a layer of indirection through an Ocaml Custom object, meaning that the common pattern of using _H() in a blocking section is unsafe. In order to fix: * Drop the _H() macro and replace it with a static inline xch_of_val(). * Opencode the assignment into Data_custom_val() in the constructors. * Rename "value xch" parameters to "value xch_val" so we can consistently have "xc_interface *xch" on the stack, and obtain the pointer with the GC lock still held. * Drop the _D() macro while at it, because it's just pointless indirection. Fixes: 8b3c06a3e545 ("tools/ocaml/xenctrl: OCaml 5 support, fix use-after-free") Signed-off-by: Edwin Török --- CC: Christian Lindig CC: David Scott CC: Edwin Török CC: Rob Hoes --- tools/ocaml/libs/xc/xenctrl_stubs.c | 454 ++++++++++++++++++++---------------- 1 file changed, 251 insertions(+), 203 deletions(-) diff --git a/tools/ocaml/libs/xc/xenctrl_stubs.c b/tools/ocaml/libs/xc/xenctrl_stubs.c index e5277f6f19a2..f9006c662382 100644 --- a/tools/ocaml/libs/xc/xenctrl_stubs.c +++ b/tools/ocaml/libs/xc/xenctrl_stubs.c @@ -37,9 +37,6 @@ #include "mmap_stubs.h" -#define _H(__h) (*((xc_interface **)Data_custom_val(__h))) -#define _D(__d) ((uint32_t)Int_val(__d)) - #ifndef Val_none #define Val_none (Val_int(0)) #endif @@ -48,9 +45,18 @@ #define Tag_some 0 #endif +static inline xc_interface *xch_of_val(value v) +{ + xc_interface *xch = *(xc_interface **)Data_custom_val(v); + + return xch; +} + static void stub_xenctrl_finalize(value v) { - xc_interface_close(_H(v)); + xc_interface *xch = xch_of_val(v); + + xc_interface_close(xch); } static struct custom_operations xenctrl_ops = { @@ -100,7 +106,7 @@ CAMLprim value stub_xc_interface_open(value unit) failwith_xc(xch); result = caml_alloc_custom(&xenctrl_ops, sizeof(xch), 0, 1); - _H(result) = xch; + *(xc_interface **)Data_custom_val(result) = xch; CAMLreturn(result); } @@ -187,10 +193,11 @@ static unsigned int ocaml_list_to_c_bitmap(value l) return val; } -CAMLprim value stub_xc_domain_create(value xch, value wanted_domid, value config) +CAMLprim value stub_xc_domain_create(value xch_val, value wanted_domid, value config) { - CAMLparam3(xch, wanted_domid, config); + CAMLparam3(xch_val, wanted_domid, config); CAMLlocal2(l, arch_domconfig); + xc_interface *xch = xch_of_val(xch_val); /* Mnemonics for the named fields inside domctl_create_config */ #define VAL_SSIDREF Field(config, 0) @@ -282,98 +289,104 @@ CAMLprim value stub_xc_domain_create(value xch, value wanted_domid, value config #undef VAL_SSIDREF caml_enter_blocking_section(); - result = xc_domain_create(_H(xch), &domid, &cfg); + result = xc_domain_create(xch, &domid, &cfg); caml_leave_blocking_section(); if (result < 0) - failwith_xc(_H(xch)); + failwith_xc(xch); CAMLreturn(Val_int(domid)); } -CAMLprim value stub_xc_domain_max_vcpus(value xch, value domid, +CAMLprim value stub_xc_domain_max_vcpus(value xch_val, value domid, value max_vcpus) { - CAMLparam3(xch, domid, max_vcpus); + CAMLparam3(xch_val, domid, max_vcpus); + xc_interface *xch = xch_of_val(xch_val); int r; - r = xc_domain_max_vcpus(_H(xch), _D(domid), Int_val(max_vcpus)); + r = xc_domain_max_vcpus(xch, Int_val(domid), Int_val(max_vcpus)); if (r) - failwith_xc(_H(xch)); + failwith_xc(xch); CAMLreturn(Val_unit); } -value stub_xc_domain_sethandle(value xch, value domid, value handle) +value stub_xc_domain_sethandle(value xch_val, value domid, value handle) { - CAMLparam3(xch, domid, handle); + CAMLparam3(xch_val, domid, handle); + xc_interface *xch = xch_of_val(xch_val); xen_domain_handle_t h; int i; domain_handle_of_uuid_string(h, String_val(handle)); - i = xc_domain_sethandle(_H(xch), _D(domid), h); + i = xc_domain_sethandle(xch, Int_val(domid), h); if (i) - failwith_xc(_H(xch)); + failwith_xc(xch); CAMLreturn(Val_unit); } -static value dom_op(value xch, value domid, int (*fn)(xc_interface *, uint32_t)) +static value dom_op(value xch_val, value domid, + int (*fn)(xc_interface *, uint32_t)) { - CAMLparam2(xch, domid); + CAMLparam2(xch_val, domid); + xc_interface *xch = xch_of_val(xch_val); int result; - uint32_t c_domid = _D(domid); + uint32_t c_domid = Int_val(domid); caml_enter_blocking_section(); - result = fn(_H(xch), c_domid); + result = fn(xch, c_domid); caml_leave_blocking_section(); if (result) - failwith_xc(_H(xch)); + failwith_xc(xch); CAMLreturn(Val_unit); } -CAMLprim value stub_xc_domain_pause(value xch, value domid) +CAMLprim value stub_xc_domain_pause(value xch_val, value domid) { - return dom_op(xch, domid, xc_domain_pause); + return dom_op(xch_val, domid, xc_domain_pause); } -CAMLprim value stub_xc_domain_unpause(value xch, value domid) +CAMLprim value stub_xc_domain_unpause(value xch_val, value domid) { - return dom_op(xch, domid, xc_domain_unpause); + return dom_op(xch_val, domid, xc_domain_unpause); } -CAMLprim value stub_xc_domain_destroy(value xch, value domid) +CAMLprim value stub_xc_domain_destroy(value xch_val, value domid) { - return dom_op(xch, domid, xc_domain_destroy); + return dom_op(xch_val, domid, xc_domain_destroy); } -CAMLprim value stub_xc_domain_resume_fast(value xch, value domid) +CAMLprim value stub_xc_domain_resume_fast(value xch_val, value domid) { - CAMLparam2(xch, domid); + CAMLparam2(xch_val, domid); + xc_interface *xch = xch_of_val(xch_val); int result; - uint32_t c_domid = _D(domid); + uint32_t c_domid = Int_val(domid); caml_enter_blocking_section(); - result = xc_domain_resume(_H(xch), c_domid, 1); + result = xc_domain_resume(xch, c_domid, 1); caml_leave_blocking_section(); if (result) - failwith_xc(_H(xch)); + failwith_xc(xch); CAMLreturn(Val_unit); } -CAMLprim value stub_xc_domain_shutdown(value xch, value domid, value reason) +CAMLprim value stub_xc_domain_shutdown(value xch_val, value domid, value reason) { - CAMLparam3(xch, domid, reason); + CAMLparam3(xch_val, domid, reason); + xc_interface *xch = xch_of_val(xch_val); int ret; - ret = xc_domain_shutdown(_H(xch), _D(domid), Int_val(reason)); + ret = xc_domain_shutdown(xch, Int_val(domid), Int_val(reason)); if (ret < 0) - failwith_xc(_H(xch)); + failwith_xc(xch); CAMLreturn(Val_unit); } @@ -433,10 +446,11 @@ static value alloc_domaininfo(xc_domaininfo_t * info) CAMLreturn(result); } -CAMLprim value stub_xc_domain_getinfolist(value xch, value first_domain, value nb) +CAMLprim value stub_xc_domain_getinfolist(value xch_val, value first_domain, value nb) { - CAMLparam3(xch, first_domain, nb); + CAMLparam3(xch_val, first_domain, nb); CAMLlocal2(result, temp); + xc_interface *xch = xch_of_val(xch_val); xc_domaininfo_t * info; int i, ret, toalloc, retval; unsigned int c_max_domains; @@ -450,16 +464,16 @@ CAMLprim value stub_xc_domain_getinfolist(value xch, value first_domain, value n result = temp = Val_emptylist; - c_first_domain = _D(first_domain); + c_first_domain = Int_val(first_domain); c_max_domains = Int_val(nb); caml_enter_blocking_section(); - retval = xc_domain_getinfolist(_H(xch), c_first_domain, + retval = xc_domain_getinfolist(xch, c_first_domain, c_max_domains, info); caml_leave_blocking_section(); if (retval < 0) { free(info); - failwith_xc(_H(xch)); + failwith_xc(xch); } for (i = 0; i < retval; i++) { result = caml_alloc_small(2, Tag_cons); @@ -474,38 +488,39 @@ CAMLprim value stub_xc_domain_getinfolist(value xch, value first_domain, value n CAMLreturn(result); } -CAMLprim value stub_xc_domain_getinfo(value xch, value domid) +CAMLprim value stub_xc_domain_getinfo(value xch_val, value domid) { - CAMLparam2(xch, domid); + CAMLparam2(xch_val, domid); CAMLlocal1(result); + xc_interface *xch = xch_of_val(xch_val); xc_domaininfo_t info; int ret; - ret = xc_domain_getinfolist(_H(xch), _D(domid), 1, &info); + ret = xc_domain_getinfolist(xch, Int_val(domid), 1, &info); if (ret != 1) - failwith_xc(_H(xch)); - if (info.domain != _D(domid)) - failwith_xc(_H(xch)); + failwith_xc(xch); + if (info.domain != Int_val(domid)) + failwith_xc(xch); result = alloc_domaininfo(&info); CAMLreturn(result); } -CAMLprim value stub_xc_vcpu_getinfo(value xch, value domid, value vcpu) +CAMLprim value stub_xc_vcpu_getinfo(value xch_val, value domid, value vcpu) { - CAMLparam3(xch, domid, vcpu); + CAMLparam3(xch_val, domid, vcpu); CAMLlocal1(result); + xc_interface *xch = xch_of_val(xch_val); xc_vcpuinfo_t info; int retval; - uint32_t c_domid = _D(domid); + uint32_t c_domid = Int_val(domid); uint32_t c_vcpu = Int_val(vcpu); caml_enter_blocking_section(); - retval = xc_vcpu_getinfo(_H(xch), c_domid, - c_vcpu, &info); + retval = xc_vcpu_getinfo(xch, c_domid, c_vcpu, &info); caml_leave_blocking_section(); if (retval < 0) - failwith_xc(_H(xch)); + failwith_xc(xch); result = caml_alloc_tuple(5); Store_field(result, 0, Val_bool(info.online)); @@ -517,17 +532,18 @@ CAMLprim value stub_xc_vcpu_getinfo(value xch, value domid, value vcpu) CAMLreturn(result); } -CAMLprim value stub_xc_vcpu_context_get(value xch, value domid, +CAMLprim value stub_xc_vcpu_context_get(value xch_val, value domid, value cpu) { - CAMLparam3(xch, domid, cpu); + CAMLparam3(xch_val, domid, cpu); + xc_interface *xch = xch_of_val(xch_val); CAMLlocal1(context); int ret; vcpu_guest_context_any_t ctxt; - ret = xc_vcpu_getcontext(_H(xch), _D(domid), Int_val(cpu), &ctxt); + ret = xc_vcpu_getcontext(xch, Int_val(domid), Int_val(cpu), &ctxt); if ( ret < 0 ) - failwith_xc(_H(xch)); + failwith_xc(xch); context = caml_alloc_string(sizeof(ctxt)); memcpy((char *) String_val(context), &ctxt.c, sizeof(ctxt.c)); @@ -535,10 +551,10 @@ CAMLprim value stub_xc_vcpu_context_get(value xch, value domid, CAMLreturn(context); } -static int get_cpumap_len(value xch, value cpumap) +static int get_cpumap_len(xc_interface *xch, value cpumap) { int ml_len = Wosize_val(cpumap); - int xc_len = xc_get_max_cpus(_H(xch)); + int xc_len = xc_get_max_cpus(xch); if (ml_len < xc_len) return ml_len; @@ -546,56 +562,58 @@ static int get_cpumap_len(value xch, value cpumap) return xc_len; } -CAMLprim value stub_xc_vcpu_setaffinity(value xch, value domid, +CAMLprim value stub_xc_vcpu_setaffinity(value xch_val, value domid, value vcpu, value cpumap) { - CAMLparam4(xch, domid, vcpu, cpumap); + CAMLparam4(xch_val, domid, vcpu, cpumap); + xc_interface *xch = xch_of_val(xch_val); int i, len = get_cpumap_len(xch, cpumap); xc_cpumap_t c_cpumap; int retval; - c_cpumap = xc_cpumap_alloc(_H(xch)); + c_cpumap = xc_cpumap_alloc(xch); if (c_cpumap == NULL) - failwith_xc(_H(xch)); + failwith_xc(xch); for (i=0; i= 0) { @@ -755,7 +779,7 @@ CAMLprim value stub_xc_readconsolering(value xch) count = size - count; caml_enter_blocking_section(); - ret = xc_readconsolering(_H(xch), str, &count, 0, 1, &index); + ret = xc_readconsolering(xch, str, &count, 0, 1, &index); caml_leave_blocking_section(); count += str - ptr; @@ -777,30 +801,32 @@ CAMLprim value stub_xc_readconsolering(value xch) CAMLreturn(ring); } -CAMLprim value stub_xc_send_debug_keys(value xch, value keys) +CAMLprim value stub_xc_send_debug_keys(value xch_val, value keys) { - CAMLparam2(xch, keys); + CAMLparam2(xch_val, keys); + xc_interface *xch = xch_of_val(xch_val); int r; - r = xc_send_debug_keys(_H(xch), String_val(keys)); + r = xc_send_debug_keys(xch, String_val(keys)); if (r) - failwith_xc(_H(xch)); + failwith_xc(xch); CAMLreturn(Val_unit); } -CAMLprim value stub_xc_physinfo(value xch) +CAMLprim value stub_xc_physinfo(value xch_val) { - CAMLparam1(xch); + CAMLparam1(xch_val); CAMLlocal4(physinfo, cap_list, arch_cap_flags, arch_cap_list); + xc_interface *xch = xch_of_val(xch_val); xc_physinfo_t c_physinfo; int r, arch_cap_flags_tag; caml_enter_blocking_section(); - r = xc_physinfo(_H(xch), &c_physinfo); + r = xc_physinfo(xch, &c_physinfo); caml_leave_blocking_section(); if (r) - failwith_xc(_H(xch)); + failwith_xc(xch); /* * capabilities: physinfo_cap_flag list; @@ -837,10 +863,11 @@ CAMLprim value stub_xc_physinfo(value xch) CAMLreturn(physinfo); } -CAMLprim value stub_xc_pcpu_info(value xch, value nr_cpus) +CAMLprim value stub_xc_pcpu_info(value xch_val, value nr_cpus) { - CAMLparam2(xch, nr_cpus); + CAMLparam2(xch_val, nr_cpus); CAMLlocal2(pcpus, v); + xc_interface *xch = xch_of_val(xch_val); xc_cpuinfo_t *info; int r, size; @@ -852,12 +879,12 @@ CAMLprim value stub_xc_pcpu_info(value xch, value nr_cpus) caml_raise_out_of_memory(); caml_enter_blocking_section(); - r = xc_getcpuinfo(_H(xch), Int_val(nr_cpus), info, &size); + r = xc_getcpuinfo(xch, Int_val(nr_cpus), info, &size); caml_leave_blocking_section(); if (r) { free(info); - failwith_xc(_H(xch)); + failwith_xc(xch); } if (size > 0) { @@ -873,79 +900,82 @@ CAMLprim value stub_xc_pcpu_info(value xch, value nr_cpus) CAMLreturn(pcpus); } -CAMLprim value stub_xc_domain_setmaxmem(value xch, value domid, +CAMLprim value stub_xc_domain_setmaxmem(value xch_val, value domid, value max_memkb) { - CAMLparam3(xch, domid, max_memkb); + CAMLparam3(xch_val, domid, max_memkb); + xc_interface *xch = xch_of_val(xch_val); int retval; - uint32_t c_domid = _D(domid); + uint32_t c_domid = Int_val(domid); unsigned int c_max_memkb = Int64_val(max_memkb); caml_enter_blocking_section(); - retval = xc_domain_setmaxmem(_H(xch), c_domid, - c_max_memkb); + retval = xc_domain_setmaxmem(xch, c_domid, c_max_memkb); caml_leave_blocking_section(); if (retval) - failwith_xc(_H(xch)); + failwith_xc(xch); CAMLreturn(Val_unit); } -CAMLprim value stub_xc_domain_set_memmap_limit(value xch, value domid, +CAMLprim value stub_xc_domain_set_memmap_limit(value xch_val, value domid, value map_limitkb) { - CAMLparam3(xch, domid, map_limitkb); + CAMLparam3(xch_val, domid, map_limitkb); + xc_interface *xch = xch_of_val(xch_val); unsigned long v; int retval; v = Int64_val(map_limitkb); - retval = xc_domain_set_memmap_limit(_H(xch), _D(domid), v); + retval = xc_domain_set_memmap_limit(xch, Int_val(domid), v); if (retval) - failwith_xc(_H(xch)); + failwith_xc(xch); CAMLreturn(Val_unit); } -CAMLprim value stub_xc_domain_memory_increase_reservation(value xch, +CAMLprim value stub_xc_domain_memory_increase_reservation(value xch_val, value domid, value mem_kb) { - CAMLparam3(xch, domid, mem_kb); + CAMLparam3(xch_val, domid, mem_kb); + xc_interface *xch = xch_of_val(xch_val); int retval; unsigned long nr_extents = ((unsigned long)(Int64_val(mem_kb))) >> (XC_PAGE_SHIFT - 10); - uint32_t c_domid = _D(domid); + uint32_t c_domid = Int_val(domid); caml_enter_blocking_section(); - retval = xc_domain_increase_reservation_exact(_H(xch), c_domid, + retval = xc_domain_increase_reservation_exact(xch, c_domid, nr_extents, 0, 0, NULL); caml_leave_blocking_section(); if (retval) - failwith_xc(_H(xch)); + failwith_xc(xch); CAMLreturn(Val_unit); } -CAMLprim value stub_xc_version_version(value xch) +CAMLprim value stub_xc_version_version(value xch_val) { - CAMLparam1(xch); + CAMLparam1(xch_val); CAMLlocal1(result); + xc_interface *xch = xch_of_val(xch_val); xen_extraversion_t extra; long packed; int retval; caml_enter_blocking_section(); - packed = xc_version(_H(xch), XENVER_version, NULL); + packed = xc_version(xch, XENVER_version, NULL); caml_leave_blocking_section(); if (packed < 0) - failwith_xc(_H(xch)); + failwith_xc(xch); caml_enter_blocking_section(); - retval = xc_version(_H(xch), XENVER_extraversion, &extra); + retval = xc_version(xch, XENVER_extraversion, &extra); caml_leave_blocking_section(); if (retval) - failwith_xc(_H(xch)); + failwith_xc(xch); result = caml_alloc_tuple(3); @@ -957,19 +987,20 @@ CAMLprim value stub_xc_version_version(value xch) } -CAMLprim value stub_xc_version_compile_info(value xch) +CAMLprim value stub_xc_version_compile_info(value xch_val) { - CAMLparam1(xch); + CAMLparam1(xch_val); CAMLlocal1(result); + xc_interface *xch = xch_of_val(xch_val); xen_compile_info_t ci; int retval; caml_enter_blocking_section(); - retval = xc_version(_H(xch), XENVER_compile_info, &ci); + retval = xc_version(xch, XENVER_compile_info, &ci); caml_leave_blocking_section(); if (retval) - failwith_xc(_H(xch)); + failwith_xc(xch); result = caml_alloc_tuple(4); @@ -982,35 +1013,36 @@ CAMLprim value stub_xc_version_compile_info(value xch) } -static value xc_version_single_string(value xch, int code, void *info) +static value xc_version_single_string(value xch_val, int code, void *info) { - CAMLparam1(xch); + CAMLparam1(xch_val); + xc_interface *xch = xch_of_val(xch_val); int retval; caml_enter_blocking_section(); - retval = xc_version(_H(xch), code, info); + retval = xc_version(xch, code, info); caml_leave_blocking_section(); if (retval) - failwith_xc(_H(xch)); + failwith_xc(xch); CAMLreturn(caml_copy_string((char *)info)); } -CAMLprim value stub_xc_version_changeset(value xch) +CAMLprim value stub_xc_version_changeset(value xch_val) { xen_changeset_info_t ci; - return xc_version_single_string(xch, XENVER_changeset, &ci); + return xc_version_single_string(xch_val, XENVER_changeset, &ci); } -CAMLprim value stub_xc_version_capabilities(value xch) +CAMLprim value stub_xc_version_capabilities(value xch_val) { xen_capabilities_info_t ci; - return xc_version_single_string(xch, XENVER_capabilities, &ci); + return xc_version_single_string(xch_val, XENVER_capabilities, &ci); } @@ -1022,11 +1054,12 @@ CAMLprim value stub_pages_to_kib(value pages) } -CAMLprim value stub_map_foreign_range(value xch, value dom, +CAMLprim value stub_map_foreign_range(value xch_val, value dom, value size, value mfn) { - CAMLparam4(xch, dom, size, mfn); + CAMLparam4(xch_val, dom, size, mfn); CAMLlocal1(result); + xc_interface *xch = xch_of_val(xch_val); struct mmap_interface *intf; unsigned long c_mfn = Nativeint_val(mfn); int len = Int_val(size); @@ -1037,7 +1070,7 @@ CAMLprim value stub_map_foreign_range(value xch, value dom, Abstract_tag); caml_enter_blocking_section(); - ptr = xc_map_foreign_range(_H(xch), _D(dom), len, + ptr = xc_map_foreign_range(xch, Int_val(dom), len, PROT_READ|PROT_WRITE, c_mfn); caml_leave_blocking_section(); @@ -1050,18 +1083,19 @@ CAMLprim value stub_map_foreign_range(value xch, value dom, CAMLreturn(result); } -CAMLprim value stub_sched_credit_domain_get(value xch, value domid) +CAMLprim value stub_sched_credit_domain_get(value xch_val, value domid) { - CAMLparam2(xch, domid); + CAMLparam2(xch_val, domid); CAMLlocal1(sdom); + xc_interface *xch = xch_of_val(xch_val); struct xen_domctl_sched_credit c_sdom; int ret; caml_enter_blocking_section(); - ret = xc_sched_credit_domain_get(_H(xch), _D(domid), &c_sdom); + ret = xc_sched_credit_domain_get(xch, Int_val(domid), &c_sdom); caml_leave_blocking_section(); if (ret != 0) - failwith_xc(_H(xch)); + failwith_xc(xch); sdom = caml_alloc_tuple(2); Store_field(sdom, 0, Val_int(c_sdom.weight)); @@ -1070,67 +1104,71 @@ CAMLprim value stub_sched_credit_domain_get(value xch, value domid) CAMLreturn(sdom); } -CAMLprim value stub_sched_credit_domain_set(value xch, value domid, +CAMLprim value stub_sched_credit_domain_set(value xch_val, value domid, value sdom) { - CAMLparam3(xch, domid, sdom); + CAMLparam3(xch_val, domid, sdom); + xc_interface *xch = xch_of_val(xch_val); struct xen_domctl_sched_credit c_sdom; int ret; c_sdom.weight = Int_val(Field(sdom, 0)); c_sdom.cap = Int_val(Field(sdom, 1)); caml_enter_blocking_section(); - ret = xc_sched_credit_domain_set(_H(xch), _D(domid), &c_sdom); + ret = xc_sched_credit_domain_set(xch, Int_val(domid), &c_sdom); caml_leave_blocking_section(); if (ret != 0) - failwith_xc(_H(xch)); + failwith_xc(xch); CAMLreturn(Val_unit); } -CAMLprim value stub_shadow_allocation_get(value xch, value domid) +CAMLprim value stub_shadow_allocation_get(value xch_val, value domid) { - CAMLparam2(xch, domid); + CAMLparam2(xch_val, domid); CAMLlocal1(mb); + xc_interface *xch = xch_of_val(xch_val); unsigned int c_mb; int ret; caml_enter_blocking_section(); - ret = xc_shadow_control(_H(xch), _D(domid), + ret = xc_shadow_control(xch, Int_val(domid), XEN_DOMCTL_SHADOW_OP_GET_ALLOCATION, &c_mb, 0); caml_leave_blocking_section(); if (ret != 0) - failwith_xc(_H(xch)); + failwith_xc(xch); mb = Val_int(c_mb); CAMLreturn(mb); } -CAMLprim value stub_shadow_allocation_set(value xch, value domid, +CAMLprim value stub_shadow_allocation_set(value xch_val, value domid, value mb) { - CAMLparam3(xch, domid, mb); + CAMLparam3(xch_val, domid, mb); + xc_interface *xch = xch_of_val(xch_val); unsigned int c_mb; int ret; c_mb = Int_val(mb); caml_enter_blocking_section(); - ret = xc_shadow_control(_H(xch), _D(domid), + ret = xc_shadow_control(xch, Int_val(domid), XEN_DOMCTL_SHADOW_OP_SET_ALLOCATION, &c_mb, 0); caml_leave_blocking_section(); if (ret != 0) - failwith_xc(_H(xch)); + failwith_xc(xch); CAMLreturn(Val_unit); } -CAMLprim value stub_xc_domain_ioport_permission(value xch, value domid, +CAMLprim value stub_xc_domain_ioport_permission(value xch_val, value domid, value start_port, value nr_ports, value allow) { - CAMLparam5(xch, domid, start_port, nr_ports, allow); + CAMLparam5(xch_val, domid, start_port, nr_ports, allow); + xc_interface *xch = xch_of_val(xch_val); uint32_t c_start_port, c_nr_ports; uint8_t c_allow; int ret; @@ -1139,19 +1177,20 @@ CAMLprim value stub_xc_domain_ioport_permission(value xch, value domid, c_nr_ports = Int_val(nr_ports); c_allow = Bool_val(allow); - ret = xc_domain_ioport_permission(_H(xch), _D(domid), + ret = xc_domain_ioport_permission(xch, Int_val(domid), c_start_port, c_nr_ports, c_allow); if (ret < 0) - failwith_xc(_H(xch)); + failwith_xc(xch); CAMLreturn(Val_unit); } -CAMLprim value stub_xc_domain_iomem_permission(value xch, value domid, +CAMLprim value stub_xc_domain_iomem_permission(value xch_val, value domid, value start_pfn, value nr_pfns, value allow) { - CAMLparam5(xch, domid, start_pfn, nr_pfns, allow); + CAMLparam5(xch_val, domid, start_pfn, nr_pfns, allow); + xc_interface *xch = xch_of_val(xch_val); unsigned long c_start_pfn, c_nr_pfns; uint8_t c_allow; int ret; @@ -1160,18 +1199,19 @@ CAMLprim value stub_xc_domain_iomem_permission(value xch, value domid, c_nr_pfns = Nativeint_val(nr_pfns); c_allow = Bool_val(allow); - ret = xc_domain_iomem_permission(_H(xch), _D(domid), + ret = xc_domain_iomem_permission(xch, Int_val(domid), c_start_pfn, c_nr_pfns, c_allow); if (ret < 0) - failwith_xc(_H(xch)); + failwith_xc(xch); CAMLreturn(Val_unit); } -CAMLprim value stub_xc_domain_irq_permission(value xch, value domid, +CAMLprim value stub_xc_domain_irq_permission(value xch_val, value domid, value pirq, value allow) { - CAMLparam4(xch, domid, pirq, allow); + CAMLparam4(xch_val, domid, pirq, allow); + xc_interface *xch = xch_of_val(xch_val); uint32_t c_pirq; bool c_allow; int ret; @@ -1179,41 +1219,44 @@ CAMLprim value stub_xc_domain_irq_permission(value xch, value domid, c_pirq = Int_val(pirq); c_allow = Bool_val(allow); - ret = xc_domain_irq_permission(_H(xch), _D(domid), + ret = xc_domain_irq_permission(xch, Int_val(domid), c_pirq, c_allow); if (ret < 0) - failwith_xc(_H(xch)); + failwith_xc(xch); CAMLreturn(Val_unit); } -CAMLprim value stub_xc_hvm_param_get(value xch, value domid, value param) +CAMLprim value stub_xc_hvm_param_get(value xch_val, value domid, value param) { - CAMLparam3(xch, domid, param); + CAMLparam3(xch_val, domid, param); + xc_interface *xch = xch_of_val(xch_val); uint64_t val; int ret; caml_enter_blocking_section(); - ret = xc_hvm_param_get(_H(xch), _D(domid), Int_val(param), &val); + ret = xc_hvm_param_get(xch, Int_val(domid), Int_val(param), &val); caml_leave_blocking_section(); if ( ret ) - failwith_xc(_H(xch)); + failwith_xc(xch); CAMLreturn(caml_copy_int64(val)); } -CAMLprim value stub_xc_hvm_param_set(value xch, value domid, value param, value val) +CAMLprim value stub_xc_hvm_param_set(value xch_val, value domid, value param, value val) { - CAMLparam4(xch, domid, param, val); + CAMLparam4(xch_val, domid, param, val); + xc_interface *xch = xch_of_val(xch_val); + uint64_t val64 = Int64_val(val); int ret; caml_enter_blocking_section(); - ret = xc_hvm_param_set(_H(xch), _D(domid), Int_val(param), Int64_val(val)); + ret = xc_hvm_param_set(xch, Int_val(domid), Int_val(param), val64); caml_leave_blocking_section(); if ( ret ) - failwith_xc(_H(xch)); + failwith_xc(xch); CAMLreturn(Val_unit); } @@ -1226,9 +1269,10 @@ static uint32_t encode_sbdf(int domain, int bus, int dev, int func) ((uint32_t)func & 0x7); } -CAMLprim value stub_xc_domain_test_assign_device(value xch, value domid, value desc) +CAMLprim value stub_xc_domain_test_assign_device(value xch_val, value domid, value desc) { - CAMLparam3(xch, domid, desc); + CAMLparam3(xch_val, domid, desc); + xc_interface *xch = xch_of_val(xch_val); int ret; int domain, bus, dev, func; uint32_t sbdf; @@ -1239,14 +1283,15 @@ CAMLprim value stub_xc_domain_test_assign_device(value xch, value domid, value d func = Int_val(Field(desc, 3)); sbdf = encode_sbdf(domain, bus, dev, func); - ret = xc_test_assign_device(_H(xch), _D(domid), sbdf); + ret = xc_test_assign_device(xch, Int_val(domid), sbdf); CAMLreturn(Val_bool(ret == 0)); } -CAMLprim value stub_xc_domain_assign_device(value xch, value domid, value desc) +CAMLprim value stub_xc_domain_assign_device(value xch_val, value domid, value desc) { - CAMLparam3(xch, domid, desc); + CAMLparam3(xch_val, domid, desc); + xc_interface *xch = xch_of_val(xch_val); int ret; int domain, bus, dev, func; uint32_t sbdf; @@ -1257,17 +1302,18 @@ CAMLprim value stub_xc_domain_assign_device(value xch, value domid, value desc) func = Int_val(Field(desc, 3)); sbdf = encode_sbdf(domain, bus, dev, func); - ret = xc_assign_device(_H(xch), _D(domid), sbdf, + ret = xc_assign_device(xch, Int_val(domid), sbdf, XEN_DOMCTL_DEV_RDM_RELAXED); if (ret < 0) - failwith_xc(_H(xch)); + failwith_xc(xch); CAMLreturn(Val_unit); } -CAMLprim value stub_xc_domain_deassign_device(value xch, value domid, value desc) +CAMLprim value stub_xc_domain_deassign_device(value xch_val, value domid, value desc) { - CAMLparam3(xch, domid, desc); + CAMLparam3(xch_val, domid, desc); + xc_interface *xch = xch_of_val(xch_val); int ret; int domain, bus, dev, func; uint32_t sbdf; @@ -1278,28 +1324,29 @@ CAMLprim value stub_xc_domain_deassign_device(value xch, value domid, value desc func = Int_val(Field(desc, 3)); sbdf = encode_sbdf(domain, bus, dev, func); - ret = xc_deassign_device(_H(xch), _D(domid), sbdf); + ret = xc_deassign_device(xch, Int_val(domid), sbdf); if (ret < 0) - failwith_xc(_H(xch)); + failwith_xc(xch); CAMLreturn(Val_unit); } -CAMLprim value stub_xc_get_cpu_featureset(value xch, value idx) +CAMLprim value stub_xc_get_cpu_featureset(value xch_val, value idx) { - CAMLparam2(xch, idx); + CAMLparam2(xch_val, idx); CAMLlocal1(bitmap_val); #if defined(__i386__) || defined(__x86_64__) + xc_interface *xch = xch_of_val(xch_val); /* Safe, because of the global ocaml lock. */ static uint32_t fs_len; if (fs_len == 0) { - int ret = xc_get_cpu_featureset(_H(xch), 0, &fs_len, NULL); + int ret = xc_get_cpu_featureset(xch, 0, &fs_len, NULL); if (ret || (fs_len == 0)) - failwith_xc(_H(xch)); + failwith_xc(xch); } { @@ -1307,10 +1354,10 @@ CAMLprim value stub_xc_get_cpu_featureset(value xch, value idx) uint32_t fs[fs_len], len = fs_len; unsigned int i; - int ret = xc_get_cpu_featureset(_H(xch), Int_val(idx), &len, fs); + int ret = xc_get_cpu_featureset(xch, Int_val(idx), &len, fs); if (ret) - failwith_xc(_H(xch)); + failwith_xc(xch); bitmap_val = caml_alloc(len, 0); @@ -1323,15 +1370,16 @@ CAMLprim value stub_xc_get_cpu_featureset(value xch, value idx) CAMLreturn(bitmap_val); } -CAMLprim value stub_xc_watchdog(value xch, value domid, value timeout) +CAMLprim value stub_xc_watchdog(value xch_val, value domid, value timeout) { - CAMLparam3(xch, domid, timeout); + CAMLparam3(xch_val, domid, timeout); + xc_interface *xch = xch_of_val(xch_val); int ret; unsigned int c_timeout = Int32_val(timeout); - ret = xc_watchdog(_H(xch), _D(domid), c_timeout); + ret = xc_watchdog(xch, Int_val(domid), c_timeout); if (ret < 0) - failwith_xc(_H(xch)); + failwith_xc(xch); CAMLreturn(Val_int(ret)); }