From nobody Tue Jan 24 23:28:25 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4P1jp16jVxz3105r; Tue, 24 Jan 2023 23:28:25 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4P1jp15vBwz3j5s; Tue, 24 Jan 2023 23:28:25 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1674602905; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=bUi4KoCZwFcd0NlPRlKWwlfMJ8DVACl7g1Ry3xjKzQE=; b=F3X+TaiapwhZUcX0QWgWoVRNzaL3xlZbJdnz7n0Y4Gu0NLIRs0Leqhl0qTLj/wbLzRTmcK lgrupIyXvYo5XXyk6XA0qu4u6YaxdMmQSXQkVvwjCAq+DYFfkdxRNUApRdbOnb8sdY0pfp fktZbq9BbIgLjWa4RkJRhjwmSqRPD8BTjGsEy8s2zlp050qaLAwkxiIr7lVrozGJhVyu1Y TRI6fP/j29mx0m6Xr/cTJS5PfKlAqg8H5B1lFzMLDECzjjq/H11ts4LChtrpuuLbs4arDi lyuyHBwOEPU/cD4K5LO2EVdhDQ7G6Nc7bOLqOXduLp96YIBtKWUKmE8qvMCVng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1674602905; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=bUi4KoCZwFcd0NlPRlKWwlfMJ8DVACl7g1Ry3xjKzQE=; b=rbBMGAYMQ/MAg+VhTj2XwHa+11NjS6BbtubyT25M86odspBdCL+t/iUpRoZI0H4gi7XjfB vfT7osci0i+nuKu7EFfjz/gDZi48jby0DidSC3mTO/TZfBRV2svJ9UbCzcCVf+vWLyrDIT PZwgwxDzgHGz43ZeCHPuBAcxDPyElO0Hi1Y9/whAetc8q++QMESzSXKzVRtBkAOd5WOZIm zT79K8jgguL7jPMa309R89GVr9e5DQdoChdqISBZGDTpYPwcBcdh6BJy079cTGENOeQt6Z O69xkKyii67JL8WsimtpHdWprjEJLSwquKSmazLf51nYIfsWNGkn5PKwvfjTZw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1674602905; a=rsa-sha256; cv=none; b=QBEb6SN+Q5aj0HiehwQLkxf9ffsB0QukIPIflWh/e8M5kI6K9Nkz0R1/JCR2Yvs5LAcZLM 4bwBGqHqXkyXwaQ+ALuL1a62SISYhThmeNjuC9NwOc8aKrUP4LOKvvKL+wZVRlD8NCEyV7 aGmy6f880aYkGpoIdX0khnxWGcRqreIfy/5LS/GjTAJu6vymY5Rh1m+YJs19GlCW8rz5uk OkMCRA9FH9COX216ylRM6v1sn0afgpBgR7dQKzEqzrxmdyUwsIsVOZKuBzUc/yp5YCHNLw HizwQ+l/1+9DxZTy/+peKRWIhntMR1mgPkC2I5uMsBo2p0DpBjclGViAt2cYXQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4P1jp14spXzSbJ; Tue, 24 Jan 2023 23:28:25 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 30ONSPWv092229; Tue, 24 Jan 2023 23:28:25 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 30ONSPp1092228; Tue, 24 Jan 2023 23:28:25 GMT (envelope-from git) Date: Tue, 24 Jan 2023 23:28:25 GMT Message-Id: <202301242328.30ONSPp1092228@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Warner Losh Subject: git: 1004da283d45 - stable/13 - linux: For better compatibility, provide compatible endian.h List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: imp X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 1004da283d45d2f343a3b2fcde48dc8c558a339a Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=1004da283d45d2f343a3b2fcde48dc8c558a339a commit 1004da283d45d2f343a3b2fcde48dc8c558a339a Author: Warner Losh AuthorDate: 2023-01-20 23:32:45 +0000 Commit: Warner Losh CommitDate: 2023-01-24 23:26:28 +0000 linux: For better compatibility, provide compatible endian.h Add endian.h. This includes sys/endian.h and then adds extra defines that glibc defines with double underscores for our _{BIG,BYTE,LITTLE,PDP}_ENDIAN macros. We also define __FLOAT_WORD_ORDER to be the same as _BYTE_ENDIAN since FreeBSD doesn't currently define this, and the default with glibc is exactly this for our platforms. Move common parts of endian.h and sys/endian.h into sys/_endian.h to limit namespace pollution from endian.h All this gives us good compatibility with Linux. There may be one or two upstreams that haven't integrated the patches I tried to send up. There are some minor differences: o The extra glibc macros are not defined. These are all controlled with either __ at the start, or only defined when glibc is being built. We also don't define macros that are used internally in glibc that would pollute the namespace. o For complete compatibility, this change must also be paired with providing a glibc-compatible byteswap.h. Sponsored by: Netflix Reviewed by: mhorne, markj, jhb Differential Revision: https://reviews.freebsd.org/D31962 (cherry picked from commit 30e0d2a51026830e5141ce3dd43854819bba910c) --- include/Makefile | 4 ++-- include/endian.h | 43 +++++++++++++++++++++++++++++++++++++++++++ sys/sys/_endian.h | 36 +++++++++++++++++++++++++++++++++++- sys/sys/endian.h | 42 ++++++------------------------------------ 4 files changed, 86 insertions(+), 39 deletions(-) diff --git a/include/Makefile b/include/Makefile index 6f30598fedab..b7aa723a562f 100644 --- a/include/Makefile +++ b/include/Makefile @@ -11,8 +11,8 @@ SUBDIR= arpa protocols rpcsvc rpc xlocale SUBDIR_PARALLEL= INCS= a.out.h ar.h assert.h bitstring.h complex.h cpio.h _ctype.h ctype.h \ db.h \ - dirent.h dlfcn.h elf.h elf-hints.h err.h fmtmsg.h fnmatch.h fstab.h \ - fts.h ftw.h getopt.h glob.h grp.h \ + dirent.h dlfcn.h elf.h elf-hints.h endian.h err.h fmtmsg.h fnmatch.h \ + fstab.h fts.h ftw.h getopt.h glob.h grp.h \ ieeefp.h ifaddrs.h \ inttypes.h iso646.h kenv.h langinfo.h libgen.h limits.h link.h \ locale.h malloc.h malloc_np.h memory.h monetary.h mpool.h mqueue.h \ diff --git a/include/endian.h b/include/endian.h new file mode 100644 index 000000000000..c7b78217cedd --- /dev/null +++ b/include/endian.h @@ -0,0 +1,43 @@ +/*- + * Copyright (c) 2021 M. Warner Losh + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +/* + * A mostly Linux/glibc-compatible endian.h + */ + +#ifndef _ENDIAN_H_ +#define _ENDIAN_H_ + +/* + * FreeBSD's sys/_endian.h is very close to the interface provided on Linux by + * glibc's endian.h. + */ +#include + +/* + * glibc uses double underscore for these symbols. Define these unconditionally. + * The compiler defines __BYTE_ORDER__ these days, so we don't do anything + * with that since sys/endian.h defines _BYTE_ORDER based on it. + */ +#define __BIG_ENDIAN _BIG_ENDIAN +#define __BYTE_ORDER _BYTE_ORDER +#define __LITTLE_ENDIAN _LITTLE_ENDIAN +#define __PDP_ENDIAN _PDP_ENDIAN + +/* + * FreeBSD's sys/endian.h and machine/endian.h doesn't define a separate + * byte order for floats. Use the host non-float byte order. + */ +#define __FLOAT_WORD_ORDER _BYTE_ORDER + +/* + * We don't define BIG_ENDI, LITTLE_ENDI, HIGH_HALF and LOW_HALF macros that + * glibc's endian.h defines since those appear to be internal to internal to + * glibc. We also don't try to emulate the various helper macros that glibc + * uses to limit namespace visibility. + */ + +#endif /* _ENDIAN_H_ */ diff --git a/sys/sys/_endian.h b/sys/sys/_endian.h index 936962cc729f..72e1a40a8deb 100644 --- a/sys/sys/_endian.h +++ b/sys/sys/_endian.h @@ -32,7 +32,7 @@ #ifndef _SYS__ENDIAN_H_ #define _SYS__ENDIAN_H_ -#ifndef _MACHINE_ENDIAN_H_ +#if !defined(_MACHINE_ENDIAN_H_) && !defined(_ENDIAN_H_) #error "sys/_endian.h should not be included directly" #endif @@ -89,4 +89,38 @@ #define __ntohs(x) ((__uint16_t)(x)) #endif +/* + * Host to big endian, host to little endian, big endian to host, and little + * endian to host byte order functions as detailed in byteorder(9). + */ +#if _BYTE_ORDER == _LITTLE_ENDIAN +#define htobe16(x) __bswap16((x)) +#define htobe32(x) __bswap32((x)) +#define htobe64(x) __bswap64((x)) +#define htole16(x) ((uint16_t)(x)) +#define htole32(x) ((uint32_t)(x)) +#define htole64(x) ((uint64_t)(x)) + +#define be16toh(x) __bswap16((x)) +#define be32toh(x) __bswap32((x)) +#define be64toh(x) __bswap64((x)) +#define le16toh(x) ((uint16_t)(x)) +#define le32toh(x) ((uint32_t)(x)) +#define le64toh(x) ((uint64_t)(x)) +#else /* _BYTE_ORDER != _LITTLE_ENDIAN */ +#define htobe16(x) ((uint16_t)(x)) +#define htobe32(x) ((uint32_t)(x)) +#define htobe64(x) ((uint64_t)(x)) +#define htole16(x) __bswap16((x)) +#define htole32(x) __bswap32((x)) +#define htole64(x) __bswap64((x)) + +#define be16toh(x) ((uint16_t)(x)) +#define be32toh(x) ((uint32_t)(x)) +#define be64toh(x) ((uint64_t)(x)) +#define le16toh(x) __bswap16((x)) +#define le32toh(x) __bswap32((x)) +#define le64toh(x) __bswap64((x)) +#endif /* _BYTE_ORDER == _LITTLE_ENDIAN */ + #endif /* _SYS__ENDIAN_H_ */ diff --git a/sys/sys/endian.h b/sys/sys/endian.h index faac0458ae2c..d91c442e9238 100644 --- a/sys/sys/endian.h +++ b/sys/sys/endian.h @@ -55,6 +55,12 @@ typedef __uint64_t uint64_t; #define _UINT64_T_DECLARED #endif +/* + * Note: While tempting to try to avoid namespace pollution from this file, + * several software packages assume these marcos are defined, even when it + * defines _POSIX_C_SOURCE to request an unpolluted namespace. + */ + /* * General byte order swapping functions. */ @@ -62,42 +68,7 @@ typedef __uint64_t uint64_t; #define bswap32(x) __bswap32(x) #define bswap64(x) __bswap64(x) -/* - * Host to big endian, host to little endian, big endian to host, and little - * endian to host byte order functions as detailed in byteorder(9). - */ -#if _BYTE_ORDER == _LITTLE_ENDIAN -#define htobe16(x) __bswap16((x)) -#define htobe32(x) __bswap32((x)) -#define htobe64(x) __bswap64((x)) -#define htole16(x) ((uint16_t)(x)) -#define htole32(x) ((uint32_t)(x)) -#define htole64(x) ((uint64_t)(x)) - -#define be16toh(x) __bswap16((x)) -#define be32toh(x) __bswap32((x)) -#define be64toh(x) __bswap64((x)) -#define le16toh(x) ((uint16_t)(x)) -#define le32toh(x) ((uint32_t)(x)) -#define le64toh(x) ((uint64_t)(x)) -#else /* _BYTE_ORDER != _LITTLE_ENDIAN */ -#define htobe16(x) ((uint16_t)(x)) -#define htobe32(x) ((uint32_t)(x)) -#define htobe64(x) ((uint64_t)(x)) -#define htole16(x) __bswap16((x)) -#define htole32(x) __bswap32((x)) -#define htole64(x) __bswap64((x)) - -#define be16toh(x) ((uint16_t)(x)) -#define be32toh(x) ((uint32_t)(x)) -#define be64toh(x) ((uint64_t)(x)) -#define le16toh(x) __bswap16((x)) -#define le32toh(x) __bswap32((x)) -#define le64toh(x) __bswap64((x)) -#endif /* _BYTE_ORDER == _LITTLE_ENDIAN */ - /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */ - static __inline uint16_t be16dec(const void *pp) { @@ -203,5 +174,4 @@ le64enc(void *pp, uint64_t u) le32enc(p, (uint32_t)(u & 0xffffffffU)); le32enc(p + 4, (uint32_t)(u >> 32)); } - #endif /* _SYS_ENDIAN_H_ */