summaryrefslogtreecommitdiffstats
path: root/sys/sys
diff options
context:
space:
mode:
authortmm <tmm@FreeBSD.org>2002-02-27 17:16:18 +0000
committertmm <tmm@FreeBSD.org>2002-02-27 17:16:18 +0000
commit3ed05b7b8966710c5cffbf0ba479dd2e07c91c0a (patch)
tree46353f300fd15b6b132ccde617d8726a2e38d224 /sys/sys
parente9d577adb2ffdd3b7300415775c1212a087ce0dc (diff)
downloadFreeBSD-src-3ed05b7b8966710c5cffbf0ba479dd2e07c91c0a.zip
FreeBSD-src-3ed05b7b8966710c5cffbf0ba479dd2e07c91c0a.tar.gz
Add the following functions/macros to support byte order conversions and
device drivers for bus system with other endinesses than the CPU (using interfaces compatible to NetBSD): - bwap16() and bswap32(). These have optimized implementations on some architectures; for those that don't, there exist generic implementations. - macros to convert from a certain byte order to host byte order and vice versa, using a naming scheme like le16toh(), htole16(). These are implemented using the bswap functions. - stream bus space access functions, which do not perform a byte order conversion (while the normal access functions would if the bus endianess differs from the CPU endianess). htons(), htonl(), ntohs() and ntohl() are implemented using the new functions above for kernel usage. None of the above interfaces is currently exported to user land. Make use of the new functions in a few places where local implementations of the same functionality existed. Reviewed by: mike, bde Tested on alpha by: mike
Diffstat (limited to 'sys/sys')
-rw-r--r--sys/sys/imgact_aout.h8
-rw-r--r--sys/sys/mchain.h23
-rw-r--r--sys/sys/param.h92
3 files changed, 105 insertions, 18 deletions
diff --git a/sys/sys/imgact_aout.h b/sys/sys/imgact_aout.h
index 4b20b87..07d03d0 100644
--- a/sys/sys/imgact_aout.h
+++ b/sys/sys/imgact_aout.h
@@ -50,13 +50,13 @@
((mag) & 0xffff) )
#define N_GETMAGIC_NET(ex) \
- (__ntohl((ex).a_midmag) & 0xffff)
+ (ntohl((ex).a_midmag) & 0xffff)
#define N_GETMID_NET(ex) \
- ((__ntohl((ex).a_midmag) >> 16) & 0x03ff)
+ ((ntohl((ex).a_midmag) >> 16) & 0x03ff)
#define N_GETFLAG_NET(ex) \
- ((__ntohl((ex).a_midmag) >> 26) & 0x3f)
+ ((ntohl((ex).a_midmag) >> 26) & 0x3f)
#define N_SETMAGIC_NET(ex,mag,mid,flag) \
- ( (ex).a_midmag = __htonl( (((flag)&0x3f)<<26) | (((mid)&0x03ff)<<16) \
+ ( (ex).a_midmag = htonl( (((flag)&0x3f)<<26) | (((mid)&0x03ff)<<16) \
| (((mag)&0xffff)) ) )
#define N_ALIGN(ex,x) \
diff --git a/sys/sys/mchain.h b/sys/sys/mchain.h
index 57278f4..7b4f7ec 100644
--- a/sys/sys/mchain.h
+++ b/sys/sys/mchain.h
@@ -36,6 +36,28 @@
#include <machine/endian.h>
+#ifdef _KERNEL
+
+/*
+ * XXX: remove these defines and change the function calls in the code. Use
+ * it unconditionally if (when) the extended byte order functions become
+ * available in user space.
+ */
+#define htoles(x) htole16((x))
+#define letohs(x) le16toh((x))
+#define htolel(x) htole32((x))
+#define letohl(x) le32toh((x))
+#define htoleq(x) htole64((int64_t)(x))
+#define letohq(x) le64toh((int64_t)(x))
+
+#define htobes(x) htobe16((x))
+#define betohs(x) be16toh((x))
+#define htobel(x) htobe32((x))
+#define betohl(x) be32toh((x))
+#define htobeq(x) htobe64((int64_t)(x))
+#define betohq(x) be64toh((int64_t)(x))
+
+#else
/*
* This macros probably belongs to the endian.h
*/
@@ -78,6 +100,7 @@ betohq(int64_t x)
#define letohl(x) ((u_int32_t)(x))
*/
#endif /* (BYTE_ORDER == LITTLE_ENDIAN) */
+#endif /* _KERNEL */
#ifdef _KERNEL
diff --git a/sys/sys/param.h b/sys/sys/param.h
index 3a2fa5f..6f13c4b 100644
--- a/sys/sys/param.h
+++ b/sys/sys/param.h
@@ -188,28 +188,92 @@
#define MAX(a,b) (((a)>(b))?(a):(b))
#endif
+#ifdef _KERNEL
/*
- * Kernel exposed versions of byteorder(3) functions.
- *
- * XXX this section should only be defined in the kernel, but some userland
- * software utilizes it.
+ * Extended byte order support functions, for kernel use only currently.
+ * First, generic implementation of the byte swapping functions for those
+ * architectures that do not have optimized variants of each.
*/
-#ifndef _BYTEORDER_FUNC_DEFINED
-#define _BYTEORDER_FUNC_DEFINED
-#define htonl(x) __htonl(x)
-#define htons(x) __htons(x)
-#define ntohl(x) __ntohl(x)
-#define ntohs(x) __ntohs(x)
+#ifndef _BSWAP16_DEFINED
+#define _BSWAP16_DEFINED
+static __inline __uint16_t
+__bswap16(__uint16_t x)
+{
+ return ((x >> 8) | ((x << 8) & 0xff00U));
+}
+#endif
+
+#ifndef _BSWAP32_DEFINED
+#define _BSWAP32_DEFINED
+static __inline __uint32_t
+__bswap32(__uint32_t x)
+{
+ return ((x >> 24) | ((x >> 8) & 0xff00U) | ((x << 8) & 0xff0000U) |
+ ((x << 24) & 0xff000000U));
+}
+#endif
+
+#ifndef _BSWAP64_DEFINED
+#define _BSWAP64_DEFINED
+static __inline __uint64_t
+__bswap64(__uint64_t x)
+{
+ return ((x >> 56) | ((x >> 40) & 0xff00UL) | ((x >> 24) & 0xff0000UL) |
+ ((x >> 8) & 0xff000000UL) | ((x << 8) & 0xff00000000UL) |
+ ((x << 24) & 0xff0000000000UL) | ((x << 40) & 0xff000000000000UL) |
+ ((x << 56)));
+}
#endif
+#define bswap16(x) __bswap16(x)
+#define bswap32(x) __bswap32(x)
+#define bswap64(x) __bswap64(x)
+
+#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 */
+
+#define htonl(x) htobe32((x))
+#define htons(x) htobe16((x))
+#define ntohl(x) be32toh((x))
+#define ntohs(x) be16toh((x))
+
+#endif /* _KERNEL */
+
/*
* XXX deprecated uppercase variants for byteorder(3) functions.
*/
#ifndef _POSIX_SOURCE
-#define NTOHL(x) ((x) = __ntohl(x))
-#define NTOHS(x) ((x) = __ntohs(x))
-#define HTONL(x) ((x) = __htonl(x))
-#define HTONS(x) ((x) = __htons(x))
+#define NTOHL(x) ((x) = ntohl(x))
+#define NTOHS(x) ((x) = ntohs(x))
+#define HTONL(x) ((x) = htonl(x))
+#define HTONS(x) ((x) = htons(x))
#endif /* _POSIX_SOURCE */
/*
OpenPOWER on IntegriCloud