From nobody Sat Nov 30 00:30:55 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1620757216; cv=none; d=zohomail.com; s=zohoarc; b=JqleqiQnLKJaKKqUfcyQlSAEsDHFgt+R9Npj47AdMv/f0g19EV6rJAjgP6eIRmw5nXnvw8MuTGh+cBMVUf2Pyxr9NhDn3U9Rm6N1fwTTCBgKcH/3pD7CMeBDTNdv9xjtNBmH179FJvJRpD9WQ1+dCMoYOgNvuiQZgoNUVCrIbMU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620757216; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Hxc+itkMS7hePZPcMNujdAgB/+1IISf4KHwqdcsY/aA=; b=mu7/wiUAM063E/hIywuvChHre+PazO1cCP1LRXVftY2KCidm9bFd780aC5bd5Fv4vWXiQooJC1Oy1PuwC3Gyo+cC5tYm/mbRB0h1VdbLpw1YJTzZCvxeOyN2nqf+jYxVwS0Q+k6H2lXch70Oi6D6mTFtgjzcEjMN2kdCzmdjPCg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1620757216865190.3050689330139; Tue, 11 May 2021 11:20:16 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.125966.237157 (Exim 4.92) (envelope-from ) id 1lgWzE-0001eh-II; Tue, 11 May 2021 18:20:04 +0000 Received: by outflank-mailman (output) from mailman id 125966.237157; Tue, 11 May 2021 18:20:04 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lgWzE-0001e4-Dp; Tue, 11 May 2021 18:20:04 +0000 Received: by outflank-mailman (input) for mailman id 125966; Tue, 11 May 2021 18:20:02 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lgWzC-0007g6-Gc for xen-devel@lists.xenproject.org; Tue, 11 May 2021 18:20:02 +0000 Received: from esa2.hc3370-68.iphmx.com (unknown [216.71.145.153]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id f5946d18-6347-4a01-b85a-ef69397626d8; Tue, 11 May 2021 18:19:52 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version 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: f5946d18-6347-4a01-b85a-ef69397626d8 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1620757192; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=J+9ZxhITGKoBgE2ifCsItTortedaZyt2nhMpStVL/so=; b=XJSfpc+dTIt6Q38wUp8db9vi/yMt1O/ebOIm0kqVeL0XyGdYI764Fsjh ZbkBMWwJhD0u87sto/loF8PF7rEdLgfN4iffGJHiHHoawUukEF1jWg/VO BkaBcKomDqjxQ7c0WolHh70un31yFtwbyggMUth5Xk01n2Iqq8vvzFPWs A=; Authentication-Results: esa2.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: eok2EWv+dnswMwHUxdnkS/1jaZMiXDFa8autCFiLG+96j29z7lRLvcNsXNXK8AE9HMbjLmZ4z4 wVpschl9SdLGmR0x9xpadQRjzhhqdMkTiqRRhcRrBmKoZTH9+EAShUW39qicA0CCB+MkreIDvG /ruM/TdhMJMeTJ4+0Y0EzGZtncOYzvvTFqSF1CDUpXnlkonquSzBgM2af8+NC5Ea7NRQk2cjEj t5EqGT6OdFNIVB7p62PqYuJrPT54JfDRbRjksqBm1dnActV1vMsNtrn0oeYWINvUt3LGJKb2a3 D1Q= X-SBRS: 5.1 X-MesageID: 43562374 X-Ironport-Server: esa2.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED IronPort-HdrOrdr: A9a23:IGpwuaylIAVs6EAzT9IgKrPw6L1zdoMgy1knxilNoHxuH/Bw9v re+cjzsCWftN9/Yh4dcLy7VpVoIkmsl6Kdg7NwAV7KZmCP1FdARLsI0WKI+UyCJ8SRzI9gPa cLSdkFNDXzZ2IK8PoTNmODYqodKNrsytHWuQ/HpU0dKT2D88tbnn9E4gDwKDwQeCB2QaAXOb C7/cR9qz+paR0sH7+G7ilsZZmkmzXT/qiWGCI7Ow== X-IronPort-AV: E=Sophos;i="5.82,291,1613451600"; d="scan'208";a="43562374" From: =?UTF-8?q?Edwin=20T=C3=B6r=C3=B6k?= To: CC: =?UTF-8?q?Edwin=20T=C3=B6r=C3=B6k?= , "Christian Lindig" , David Scott , "Ian Jackson" , Wei Liu Subject: [PATCH v2 11/17] tools/ocaml/libs/mmap: Expose stub_mmap_alloc Date: Tue, 11 May 2021 19:05:24 +0100 Message-ID: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) This also handles mmap errors better by using the `uerror` helper to raise a proper exception using `errno`. Changed type of `len` from `int` to `size_t`: at construction time we ensure the length is >=3D 0, so we can reflect this by using an unsigned type. The type is unsigned at the C API level, and a negative integer would just get translated to a very large unsigned number otherwise. mmap also takes off_t and size_t, so using int64 would be more generic here, however we only ever use this interface to map rings, so keeping the `int` sizes is fine. OCaml itself only uses `ints` for mapping bigarrays, and int64 for just the offset. Signed-off-by: Edwin T=C3=B6r=C3=B6k --- tools/ocaml/libs/mmap/mmap_stubs.h | 4 +++- tools/ocaml/libs/mmap/xenmmap_stubs.c | 31 +++++++++++++++++---------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/tools/ocaml/libs/mmap/mmap_stubs.h b/tools/ocaml/libs/mmap/mma= p_stubs.h index 816ba6a724..3352594e38 100644 --- a/tools/ocaml/libs/mmap/mmap_stubs.h +++ b/tools/ocaml/libs/mmap/mmap_stubs.h @@ -27,7 +27,7 @@ struct mmap_interface { void *addr; - int len; + size_t len; }; =20 #ifndef Data_abstract_val @@ -37,4 +37,6 @@ struct mmap_interface #define Intf_val(a) ((struct mmap_interface *) Data_abstract_val(a)) #define Intf_data_val(a) (Intf_val(a)->addr) =20 +value stub_mmap_alloc(void *addr, size_t len); + #endif diff --git a/tools/ocaml/libs/mmap/xenmmap_stubs.c b/tools/ocaml/libs/mmap/= xenmmap_stubs.c index 4d09c5a6e6..d7a97c76f5 100644 --- a/tools/ocaml/libs/mmap/xenmmap_stubs.c +++ b/tools/ocaml/libs/mmap/xenmmap_stubs.c @@ -27,16 +27,18 @@ #include #include #include +#include =20 #define Wsize_bsize_round(n) (Wsize_bsize( (n) + sizeof(value) - 1 )) =20 -static int mmap_interface_init(struct mmap_interface *intf, - int fd, int pflag, int mflag, - int len, int offset) +value stub_mmap_alloc(void *addr, size_t len) { - intf->len =3D len; - intf->addr =3D mmap(NULL, len, pflag, mflag, fd, offset); - return (intf->addr =3D=3D MAP_FAILED) ? errno : 0; + CAMLparam0(); + CAMLlocal1(result); + result =3D caml_alloc(Wsize_bsize_round(sizeof(struct mmap_interface)), A= bstract_tag); + Intf_val(result)->addr =3D addr; + Intf_val(result)->len =3D len; + CAMLreturn(result); } =20 CAMLprim value stub_mmap_init(value fd, value pflag, value mflag, @@ -45,6 +47,8 @@ CAMLprim value stub_mmap_init(value fd, value pflag, valu= e mflag, CAMLparam5(fd, pflag, mflag, len, offset); CAMLlocal1(result); int c_pflag, c_mflag; + void* addr; + size_t length; =20 switch (Int_val(pflag)) { case 0: c_pflag =3D PROT_READ; break; @@ -59,12 +63,17 @@ CAMLprim value stub_mmap_init(value fd, value pflag, va= lue mflag, default: caml_invalid_argument("maptype"); } =20 - result =3D caml_alloc(Wsize_bsize_round(sizeof(struct mmap_interface)), A= bstract_tag); + if (Int_val(len) < 0) + caml_invalid_argument("negative size"); + if (Int_val(offset) < 0) + caml_invalid_argument("negative offset"); + length =3D Int_val(len); =20 - if (mmap_interface_init(Intf_val(result), Int_val(fd), - c_pflag, c_mflag, - Int_val(len), Int_val(offset))) - caml_failwith("mmap"); + addr =3D mmap(NULL, length, c_pflag, c_mflag, Int_val(fd), Int_val(offset= )); + if (MAP_FAILED =3D=3D addr) + uerror("mmap", Nothing); + + result =3D stub_mmap_alloc(addr, length); CAMLreturn(result); } =20 --=20 2.25.1