xref: /xnu-11215/libkern/libkern/arm/OSByteOrder.h (revision 8d741a5d)
1 /*
2  * Copyright (c) 1999-2023 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #ifndef _OS_OSBYTEORDERARM_H
30 #define _OS_OSBYTEORDERARM_H
31 
32 #if defined (__arm__) || defined(__arm64__)
33 
34 #include <stdint.h>
35 #include <libkern/arm/_OSByteOrder.h>
36 #include <sys/_types/_os_inline.h>
37 #include <arm/arch.h> /* for _ARM_ARCH_6 */
38 
39 /* Functions for byte reversed loads. */
40 
41 struct _OSUnalignedU16 {
42 	volatile uint16_t __val;
43 } __attribute__((__packed__));
44 
45 struct _OSUnalignedU32 {
46 	volatile uint32_t __val;
47 } __attribute__((__packed__));
48 
49 struct _OSUnalignedU64 {
50 	volatile uint64_t __val;
51 } __attribute__((__packed__));
52 
53 #if defined(_POSIX_C_SOURCE) || defined(_XOPEN_SOURCE)
54 OS_INLINE
55 uint16_t
_OSReadSwapInt16(const volatile void * _base,uintptr_t _offset)56 _OSReadSwapInt16(
57 	const volatile void   * _base,
58 	uintptr_t       _offset
59 	)
60 {
61 	return _OSSwapInt16(((struct _OSUnalignedU16 *)((uintptr_t)_base + _offset))->__val);
62 }
63 #else
64 OS_INLINE
65 uint16_t
OSReadSwapInt16(const volatile void * _base,uintptr_t _offset)66 OSReadSwapInt16(
67 	const volatile void   * _base,
68 	uintptr_t       _offset
69 	)
70 {
71 	return _OSSwapInt16(((struct _OSUnalignedU16 *)((uintptr_t)_base + _offset))->__val);
72 }
73 #endif
74 
75 #if defined(_POSIX_C_SOURCE) || defined(_XOPEN_SOURCE)
76 OS_INLINE
77 uint32_t
_OSReadSwapInt32(const volatile void * _base,uintptr_t _offset)78 _OSReadSwapInt32(
79 	const volatile void   * _base,
80 	uintptr_t       _offset
81 	)
82 {
83 	return _OSSwapInt32(((struct _OSUnalignedU32 *)((uintptr_t)_base + _offset))->__val);
84 }
85 #else
86 OS_INLINE
87 uint32_t
OSReadSwapInt32(const volatile void * _base,uintptr_t _offset)88 OSReadSwapInt32(
89 	const volatile void   * _base,
90 	uintptr_t       _offset
91 	)
92 {
93 	return _OSSwapInt32(((struct _OSUnalignedU32 *)((uintptr_t)_base + _offset))->__val);
94 }
95 #endif
96 
97 #if defined(_POSIX_C_SOURCE) || defined(_XOPEN_SOURCE)
98 OS_INLINE
99 uint64_t
_OSReadSwapInt64(const volatile void * _base,uintptr_t _offset)100 _OSReadSwapInt64(
101 	const volatile void   * _base,
102 	uintptr_t       _offset
103 	)
104 {
105 	return _OSSwapInt64(((struct _OSUnalignedU64 *)((uintptr_t)_base + _offset))->__val);
106 }
107 #else
108 OS_INLINE
109 uint64_t
OSReadSwapInt64(const volatile void * _base,uintptr_t _offset)110 OSReadSwapInt64(
111 	const volatile void   * _base,
112 	uintptr_t       _offset
113 	)
114 {
115 	return _OSSwapInt64(((struct _OSUnalignedU64 *)((uintptr_t)_base + _offset))->__val);
116 }
117 #endif
118 
119 /* Functions for byte reversed stores. */
120 
121 #if defined(_POSIX_C_SOURCE) || defined(_XOPEN_SOURCE)
122 OS_INLINE
123 void
_OSWriteSwapInt16(volatile void * _base,uintptr_t _offset,uint16_t _data)124 _OSWriteSwapInt16(
125 	volatile void   * _base,
126 	uintptr_t       _offset,
127 	uint16_t        _data
128 	)
129 {
130 	((struct _OSUnalignedU16 *)((uintptr_t)_base + _offset))->__val = _OSSwapInt16(_data);
131 }
132 #else
133 OS_INLINE
134 void
OSWriteSwapInt16(volatile void * _base,uintptr_t _offset,uint16_t _data)135 OSWriteSwapInt16(
136 	volatile void   * _base,
137 	uintptr_t       _offset,
138 	uint16_t        _data
139 	)
140 {
141 	((struct _OSUnalignedU16 *)((uintptr_t)_base + _offset))->__val = _OSSwapInt16(_data);
142 }
143 #endif
144 
145 #if defined(_POSIX_C_SOURCE) || defined(_XOPEN_SOURCE)
146 OS_INLINE
147 void
_OSWriteSwapInt32(volatile void * _base,uintptr_t _offset,uint32_t _data)148 _OSWriteSwapInt32(
149 	volatile void   * _base,
150 	uintptr_t       _offset,
151 	uint32_t        _data
152 	)
153 {
154 	((struct _OSUnalignedU32 *)((uintptr_t)_base + _offset))->__val = _OSSwapInt32(_data);
155 }
156 #else
157 OS_INLINE
158 void
OSWriteSwapInt32(volatile void * _base,uintptr_t _offset,uint32_t _data)159 OSWriteSwapInt32(
160 	volatile void   * _base,
161 	uintptr_t       _offset,
162 	uint32_t        _data
163 	)
164 {
165 	((struct _OSUnalignedU32 *)((uintptr_t)_base + _offset))->__val = _OSSwapInt32(_data);
166 }
167 #endif
168 
169 #if defined(_POSIX_C_SOURCE) || defined(_XOPEN_SOURCE)
170 OS_INLINE
171 void
_OSWriteSwapInt64(volatile void * _base,uintptr_t _offset,uint64_t _data)172 _OSWriteSwapInt64(
173 	volatile void    * _base,
174 	uintptr_t        _offset,
175 	uint64_t         _data
176 	)
177 {
178 	((struct _OSUnalignedU64 *)((uintptr_t)_base + _offset))->__val = _OSSwapInt64(_data);
179 }
180 #else
181 OS_INLINE
182 void
OSWriteSwapInt64(volatile void * _base,uintptr_t _offset,uint64_t _data)183 OSWriteSwapInt64(
184 	volatile void    * _base,
185 	uintptr_t        _offset,
186 	uint64_t         _data
187 	)
188 {
189 	((struct _OSUnalignedU64 *)((uintptr_t)_base + _offset))->__val = _OSSwapInt64(_data);
190 }
191 #endif
192 
193 #endif /* defined (__arm__) || defined(__arm64__) */
194 
195 #endif /* ! _OS_OSBYTEORDERARM_H */
196