xref: /optee_os/lib/libutils/ext/include/compiler.h (revision 7124d4973d3825e67db3b4126e4d42f6b55193f8)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  */
5 
6 #ifndef COMPILER_H
7 #define COMPILER_H
8 
9 /*
10  * Macros that should be used instead of using __attribute__ directly to
11  * ease portability and make the code easier to read.
12  *
13  * Some of the defines below is known to sometimes cause conflicts when
14  * this file is included from xtest in normal world. It is assumed that
15  * the conflicting defines has the same meaning in that environment.
16  * Surrounding the troublesome defines with #ifndef should be enough.
17  */
18 #define __deprecated	__attribute__((deprecated))
19 #ifndef __packed
20 #define __packed	__attribute__((packed))
21 #endif
22 #define __weak		__attribute__((weak))
23 #ifndef __noreturn
24 #define __noreturn	__attribute__((__noreturn__))
25 #endif
26 #define __pure		__attribute__((pure))
27 #define __aligned(x)	__attribute__((aligned(x)))
28 #define __printf(a, b)	__attribute__((format(printf, a, b)))
29 #define __noinline	__attribute__((noinline))
30 #define __attr_const	__attribute__((__const__))
31 #ifndef __unused
32 #define __unused	__attribute__((unused))
33 #endif
34 #define __maybe_unused	__attribute__((unused))
35 #ifndef __used
36 #define __used		__attribute__((__used__))
37 #endif
38 #define __must_check	__attribute__((warn_unused_result))
39 #define __cold		__attribute__((__cold__))
40 #define __section(x)	__attribute__((section(x)))
41 #define __data		__section(".data")
42 #define __bss		__section(".bss")
43 #ifdef __clang__
44 #define __SECTION_FLAGS_RODATA
45 #else
46 /*
47  * Override sections flags/type generated by the C compiler to make sure they
48  * are: "a",%progbits (thus creating an allocatable, non-writeable, non-
49  * executable data section).
50  * The trailing COMMENT_CHAR comments out the flags generated by the compiler.
51  * This avoids a harmless warning with GCC.
52  */
53 #if defined(__aarch64__) || defined(__arm__)
54 #define COMMENT_CHAR "//"
55 #else
56 #define COMMENT_CHAR "#"
57 #endif
58 #define __SECTION_FLAGS_RODATA ",\"a\",%progbits " COMMENT_CHAR
59 #endif
60 #define __rodata	__section(".rodata" __SECTION_FLAGS_RODATA)
61 #define __rodata_dummy	__section(".rodata.dummy" __SECTION_FLAGS_RODATA)
62 #define __rodata_unpaged(x) \
63 	__section(".rodata.__unpaged." x __SECTION_FLAGS_RODATA)
64 #ifdef CFG_VIRTUALIZATION
65 #define __nex_bss		__section(".nex_bss")
66 #define __nex_data		__section(".nex_data")
67 #else  /* CFG_VIRTUALIZATION */
68 #define __nex_bss
69 #define __nex_data
70 #endif	/* CFG_VIRTUALIZATION */
71 #define __noprof	__attribute__((no_instrument_function))
72 #define __nostackcheck	__attribute__((no_instrument_function))
73 
74 #define __compiler_bswap64(x)	__builtin_bswap64((x))
75 #define __compiler_bswap32(x)	__builtin_bswap32((x))
76 #define __compiler_bswap16(x)	__builtin_bswap16((x))
77 
78 #define __GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
79 		       __GNUC_PATCHLEVEL__)
80 
81 #if __GCC_VERSION >= 50100 && !defined(__CHECKER__)
82 #define __HAVE_BUILTIN_OVERFLOW 1
83 #endif
84 
85 #ifdef __HAVE_BUILTIN_OVERFLOW
86 #define __compiler_add_overflow(a, b, res) \
87 	__builtin_add_overflow((a), (b), (res))
88 
89 #define __compiler_sub_overflow(a, b, res) \
90 	__builtin_sub_overflow((a), (b), (res))
91 
92 #define __compiler_mul_overflow(a, b, res) \
93 	__builtin_mul_overflow((a), (b), (res))
94 #else /*!__HAVE_BUILTIN_OVERFLOW*/
95 
96 /*
97  * Copied/inspired from https://www.fefe.de/intof.html
98  */
99 
100 #define __INTOF_ASSIGN(dest, src) (__extension__({ \
101 	typeof(src) __intof_x = (src); \
102 	typeof(dest) __intof_y = __intof_x; \
103 	(((uintmax_t)__intof_x == (uintmax_t)__intof_y) && \
104 	 ((__intof_x < 1) == (__intof_y < 1)) ? \
105 		(void)((dest) = __intof_y) , 0 : 1); \
106 }))
107 
108 #define __INTOF_ADD(c, a, b) (__extension__({ \
109 	typeof(a) __intofa_a = (a); \
110 	typeof(b) __intofa_b = (b); \
111 	intmax_t __intofa_a_signed = __intofa_a; \
112 	uintmax_t __intofa_a_unsigned = __intofa_a; \
113 	intmax_t __intofa_b_signed = __intofa_b; \
114 	uintmax_t __intofa_b_unsigned = __intofa_b; \
115 	\
116 	__intofa_b < 1 ? \
117 		__intofa_a < 1 ? \
118 			((INTMAX_MIN - __intofa_b_signed <= \
119 			  __intofa_a_signed)) ? \
120 				__INTOF_ASSIGN((c), __intofa_a_signed + \
121 						    __intofa_b_signed) : 1 \
122 		: \
123 			((__intofa_a_unsigned >= (uintmax_t)-__intofa_b) ? \
124 				__INTOF_ASSIGN((c), __intofa_a_unsigned + \
125 						    __intofa_b_signed) \
126 			: \
127 				__INTOF_ASSIGN((c), \
128 					(intmax_t)(__intofa_a_unsigned + \
129 						   __intofa_b_signed))) \
130 	: \
131 		__intofa_a < 1 ? \
132 			((__intofa_b_unsigned >= (uintmax_t)-__intofa_a) ? \
133 				__INTOF_ASSIGN((c), __intofa_a_signed + \
134 						    __intofa_b_unsigned) \
135 			: \
136 				__INTOF_ASSIGN((c), \
137 					(intmax_t)(__intofa_a_signed + \
138 						   __intofa_b_unsigned))) \
139 		: \
140 			((UINTMAX_MAX - __intofa_b_unsigned >= \
141 			  __intofa_a_unsigned) ? \
142 				__INTOF_ASSIGN((c), __intofa_a_unsigned + \
143 						    __intofa_b_unsigned) : 1); \
144 }))
145 
146 #define __INTOF_SUB(c, a, b) (__extension__({ \
147 	typeof(a) __intofs_a = a; \
148 	typeof(b) __intofs_b = b; \
149 	intmax_t __intofs_a_signed = __intofs_a; \
150 	uintmax_t __intofs_a_unsigned = __intofs_a; \
151 	intmax_t __intofs_b_signed = __intofs_b; \
152 	uintmax_t __intofs_b_unsigned = __intofs_b; \
153 	\
154 	__intofs_b < 1 ? \
155 		__intofs_a < 1 ? \
156 			((INTMAX_MAX + __intofs_b_signed >= \
157 			  __intofs_a_signed) ? \
158 				__INTOF_ASSIGN((c), __intofs_a_signed - \
159 						    __intofs_b_signed) : 1) \
160 		: \
161 			(((uintmax_t)(UINTMAX_MAX + __intofs_b_signed) >= \
162 			  __intofs_a_unsigned) ? \
163 				__INTOF_ASSIGN((c), __intofs_a - \
164 						    __intofs_b) : 1) \
165 	: \
166 		__intofs_a < 1 ? \
167 			(((intmax_t)(INTMAX_MIN + __intofs_b) <= \
168 			  __intofs_a_signed) ? \
169 				__INTOF_ASSIGN((c), \
170 					(intmax_t)(__intofs_a_signed - \
171 						   __intofs_b_unsigned)) : 1) \
172 		: \
173 			((__intofs_b_unsigned <= __intofs_a_unsigned) ? \
174 				__INTOF_ASSIGN((c), __intofs_a_unsigned - \
175 						    __intofs_b_unsigned) \
176 			: \
177 				__INTOF_ASSIGN((c), \
178 					(intmax_t)(__intofs_a_unsigned - \
179 						   __intofs_b_unsigned))); \
180 }))
181 
182 /*
183  * Dealing with detecting overflow in multiplication of integers.
184  *
185  * First step is to remove two corner cases with the minum signed integer
186  * which can't be represented as a positive integer + sign.
187  * Multiply with 0 or 1 can't overflow, no checking needed of the operation,
188  * only if it can be assigned to the result.
189  *
190  * After the corner cases are eliminated we convert the two factors to
191  * positive unsigned values, keeping track of the original in another
192  * variable which is used at the end to determine the sign of the product.
193  *
194  * The two terms (a and b) are divided into upper and lower half (x1 upper
195  * and x0 lower), so the product is:
196  * ((a1 << hshift) + a0) * ((b1 << hshift) + b0)
197  * which also is:
198  * ((a1 * b1) << (hshift * 2)) +				(T1)
199  * ((a1 * b0 + a0 * b1) << hshift) +				(T2)
200  * (a0 * b0)							(T3)
201  *
202  * From this we can tell and (a1 * b1) has to be 0 or we'll overflow, that
203  * is, at least one of a1 or b1 has to be 0. Once this has been checked the
204  * addition: ((a1 * b0) << hshift) + ((a0 * b1) << hshift)
205  * isn't an addition as one of the terms will be 0.
206  *
207  * Since each factor in: (a0 * b0)
208  * only uses half the capicity of the underlaying type it can't overflow
209  *
210  * The addition of T2 and T3 can overflow so we use __INTOF_ADD() to
211  * perform that addition. If the addition succeeds without overflow the
212  * result is assigned the required sign and checked for overflow again.
213  */
214 
215 #define __intof_mul_negate	((__intof_oa < 1) != (__intof_ob < 1))
216 #define __intof_mul_hshift	(sizeof(uintmax_t) * 8 / 2)
217 #define __intof_mul_hmask	(UINTMAX_MAX >> __intof_mul_hshift)
218 #define __intof_mul_a0		((uintmax_t)(__intof_a) >> __intof_mul_hshift)
219 #define __intof_mul_b0		((uintmax_t)(__intof_b) >> __intof_mul_hshift)
220 #define __intof_mul_a1		((uintmax_t)(__intof_a) & __intof_mul_hmask)
221 #define __intof_mul_b1		((uintmax_t)(__intof_b) & __intof_mul_hmask)
222 #define __intof_mul_t		(__intof_mul_a1 * __intof_mul_b0 + \
223 				 __intof_mul_a0 * __intof_mul_b1)
224 
225 #define __INTOF_MUL(c, a, b) (__extension__({ \
226 	typeof(a) __intof_oa = (a); \
227 	typeof(a) __intof_a = __intof_oa < 1 ? -__intof_oa : __intof_oa; \
228 	typeof(b) __intof_ob = (b); \
229 	typeof(b) __intof_b = __intof_ob < 1 ? -__intof_ob : __intof_ob; \
230 	typeof(c) __intof_c; \
231 	\
232 	__intof_oa == 0 || __intof_ob == 0 || \
233 	__intof_oa == 1 || __intof_ob == 1 ? \
234 		__INTOF_ASSIGN((c), __intof_oa * __intof_ob) : \
235 	(__intof_mul_a0 && __intof_mul_b0) || \
236 	 __intof_mul_t > __intof_mul_hmask ?  1 : \
237 	__INTOF_ADD((__intof_c), __intof_mul_t << __intof_mul_hshift, \
238 				 __intof_mul_a1 * __intof_mul_b1) ? 1 : \
239 	__intof_mul_negate ? __INTOF_ASSIGN((c), -__intof_c) : \
240 			     __INTOF_ASSIGN((c), __intof_c); \
241 }))
242 
243 #define __compiler_add_overflow(a, b, res) __INTOF_ADD(*(res), (a), (b))
244 #define __compiler_sub_overflow(a, b, res) __INTOF_SUB(*(res), (a), (b))
245 #define __compiler_mul_overflow(a, b, res) __INTOF_MUL(*(res), (a), (b))
246 
247 #endif /*!__HAVE_BUILTIN_OVERFLOW*/
248 
249 #define __compiler_compare_and_swap(p, oval, nval) \
250 	__atomic_compare_exchange_n((p), (oval), (nval), true, \
251 				    __ATOMIC_ACQUIRE, __ATOMIC_RELAXED) \
252 
253 #define __compiler_atomic_load(p) __atomic_load_n((p), __ATOMIC_RELAXED)
254 #define __compiler_atomic_store(p, val) \
255 	__atomic_store_n((p), (val), __ATOMIC_RELAXED)
256 
257 #define barrier() asm volatile ("" : : : "memory")
258 
259 #ifndef __has_attribute
260 #define __has_attribute(x) 0
261 #endif
262 
263 #if __has_attribute(__fallthrough__)
264 #define fallthrough __attribute__((__fallthrough__))
265 #else
266 #define fallthrough do {} while (0) /* fallthrough */
267 #endif
268 
269 #endif /*COMPILER_H*/
270