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