1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_COMPILER_TYPES_H
3 #define __LINUX_COMPILER_TYPES_H
4 
5 #ifndef __ASSEMBLY__
6 
7 #ifdef __CHECKER__
8 # define __user		__attribute__((noderef, address_space(1)))
9 # define __kernel	__attribute__((address_space(0)))
10 # define __safe		__attribute__((safe))
11 # define __force	__attribute__((force))
12 # define __nocast	__attribute__((nocast))
13 # define __iomem	__attribute__((noderef, address_space(2)))
14 # define __must_hold(x)	__attribute__((context(x,1,1)))
15 # define __acquires(x)	__attribute__((context(x,0,1)))
16 # define __releases(x)	__attribute__((context(x,1,0)))
17 # define __acquire(x)	__context__(x,1)
18 # define __release(x)	__context__(x,-1)
19 # define __cond_lock(x,c)	((c) ? ({ __acquire(x); 1; }) : 0)
20 # define __percpu	__attribute__((noderef, address_space(3)))
21 # define __rcu		__attribute__((noderef, address_space(4)))
22 # define __private	__attribute__((noderef))
23 extern void __chk_user_ptr(const volatile void __user *);
24 extern void __chk_io_ptr(const volatile void __iomem *);
25 # define ACCESS_PRIVATE(p, member) (*((typeof((p)->member) __force *) &(p)->member))
26 #else /* __CHECKER__ */
27 # ifdef STRUCTLEAK_PLUGIN
28 #  define __user __attribute__((user))
29 # else
30 #  define __user
31 # endif
32 # define __kernel
33 # define __safe
34 # define __force
35 # define __nocast
36 # define __iomem
37 # define __chk_user_ptr(x) (void)0
38 # define __chk_io_ptr(x) (void)0
39 # define __builtin_warning(x, y...) (1)
40 # define __must_hold(x)
41 # define __acquires(x)
42 # define __releases(x)
43 # define __acquire(x) (void)0
44 # define __release(x) (void)0
45 # define __cond_lock(x,c) (c)
46 # define __percpu
47 # define __rcu
48 # define __private
49 # define ACCESS_PRIVATE(p, member) ((p)->member)
50 #endif /* __CHECKER__ */
51 
52 /* Indirect macros required for expanded argument pasting, eg. __LINE__. */
53 #define ___PASTE(a,b) a##b
54 #define __PASTE(a,b) ___PASTE(a,b)
55 
56 #ifdef __KERNEL__
57 
58 /* Attributes */
59 #include <linux/compiler_attributes.h>
60 
61 /* Compiler specific macros. */
62 #ifdef __clang__
63 #include <linux/compiler-clang.h>
64 #elif defined(__INTEL_COMPILER)
65 #include <linux/compiler-intel.h>
66 #elif defined(__GNUC__)
67 /* The above compilers also define __GNUC__, so order is important here. */
68 #include <linux/compiler-gcc.h>
69 #else
70 #error "Unknown compiler"
71 #endif
72 
73 /*
74  * At least gcc 5.1 or clang 8 are needed.
75  */
76 #ifndef COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW
77 #error Unsupported compiler
78 #endif
79 
80 /*
81  * Some architectures need to provide custom definitions of macros provided
82  * by linux/compiler-*.h, and can do so using asm/compiler.h. We include that
83  * conditionally rather than using an asm-generic wrapper in order to avoid
84  * build failures if any C compilation, which will include this file via an
85  * -include argument in c_flags, occurs prior to the asm-generic wrappers being
86  * generated.
87  */
88 #ifdef CONFIG_HAVE_ARCH_COMPILER_H
89 #include <asm/compiler.h>
90 #endif
91 
92 struct ftrace_branch_data {
93 	const char *func;
94 	const char *file;
95 	unsigned line;
96 	union {
97 		struct {
98 			unsigned long correct;
99 			unsigned long incorrect;
100 		};
101 		struct {
102 			unsigned long miss;
103 			unsigned long hit;
104 		};
105 		unsigned long miss_hit[2];
106 	};
107 };
108 
109 struct ftrace_likely_data {
110 	struct ftrace_branch_data	data;
111 	unsigned long			constant;
112 };
113 
114 #ifdef CONFIG_ENABLE_MUST_CHECK
115 #define __must_check		__attribute__((__warn_unused_result__))
116 #else
117 #define __must_check
118 #endif
119 
120 #if defined(CC_USING_HOTPATCH)
121 #define notrace			__attribute__((hotpatch(0, 0)))
122 #elif defined(CC_USING_PATCHABLE_FUNCTION_ENTRY)
123 #define notrace			__attribute__((patchable_function_entry(0, 0)))
124 #else
125 #define notrace			__attribute__((__no_instrument_function__))
126 #endif
127 
128 /*
129  * it doesn't make sense on ARM (currently the only user of __naked)
130  * to trace naked functions because then mcount is called without
131  * stack and frame pointer being set up and there is no chance to
132  * restore the lr register to the value before mcount was called.
133  */
134 #define __naked			__attribute__((__naked__)) notrace
135 
136 #define __compiler_offsetof(a, b)	__builtin_offsetof(a, b)
137 
138 /*
139  * Force always-inline if the user requests it so via the .config.
140  * Prefer gnu_inline, so that extern inline functions do not emit an
141  * externally visible function. This makes extern inline behave as per gnu89
142  * semantics rather than c99. This prevents multiple symbol definition errors
143  * of extern inline functions at link time.
144  * A lot of inline functions can cause havoc with function tracing.
145  * Do not use __always_inline here, since currently it expands to inline again
146  * (which would break users of __always_inline).
147  */
148 #if !CONFIG_IS_ENABLED(OPTIMIZE_INLINING)
149 #define inline inline __attribute__((__always_inline__)) __gnu_inline \
150 	__inline_maybe_unused notrace
151 #else
152 #define inline inline                                    __gnu_inline \
153 	__inline_maybe_unused notrace
154 #endif
155 
156 /*
157  * gcc provides both __inline__ and __inline as alternate spellings of
158  * the inline keyword, though the latter is undocumented. New kernel
159  * code should only use the inline spelling, but some existing code
160  * uses __inline__. Since we #define inline above, to ensure
161  * __inline__ has the same semantics, we need this #define.
162  *
163  * However, the spelling __inline is strictly reserved for referring
164  * to the bare keyword.
165  */
166 #define __inline__ inline
167 
168 /*
169  * GCC does not warn about unused static inline functions for -Wunused-function.
170  * Suppress the warning in clang as well by using __maybe_unused, but enable it
171  * for W=1 build. This will allow clang to find unused functions. Remove the
172  * __inline_maybe_unused entirely after fixing most of -Wunused-function warnings.
173  */
174 #ifdef KBUILD_EXTRA_WARN1
175 #define __inline_maybe_unused
176 #else
177 #define __inline_maybe_unused __maybe_unused
178 #endif
179 
180 /*
181  * Rather then using noinline to prevent stack consumption, use
182  * noinline_for_stack instead.  For documentation reasons.
183  */
184 #define noinline_for_stack noinline
185 
186 #endif /* __KERNEL__ */
187 
188 #endif /* __ASSEMBLY__ */
189 
190 /*
191  * The below symbols may be defined for one or more, but not ALL, of the above
192  * compilers. We don't consider that to be an error, so set them to nothing.
193  * For example, some of them are for compiler specific plugins.
194  */
195 #ifndef __latent_entropy
196 # define __latent_entropy
197 #endif
198 
199 #ifndef __randomize_layout
200 # define __randomize_layout __designated_init
201 #endif
202 
203 #ifndef __no_randomize_layout
204 # define __no_randomize_layout
205 #endif
206 
207 #ifndef randomized_struct_fields_start
208 # define randomized_struct_fields_start
209 # define randomized_struct_fields_end
210 #endif
211 
212 #ifndef asm_volatile_goto
213 #define asm_volatile_goto(x...) asm goto(x)
214 #endif
215 
216 #ifdef CONFIG_CC_HAS_ASM_INLINE
217 #define asm_inline asm __inline
218 #else
219 #define asm_inline asm
220 #endif
221 
222 #ifndef __no_fgcse
223 # define __no_fgcse
224 #endif
225 
226 /* Are two types/vars the same type (ignoring qualifiers)? */
227 #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
228 
229 /* Is this type a native word size -- useful for atomic operations */
230 #define __native_word(t) \
231 	(sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || \
232 	 sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
233 
234 /* Helpers for emitting diagnostics in pragmas. */
235 #ifndef __diag
236 #define __diag(string)
237 #endif
238 
239 #ifndef __diag_GCC
240 #define __diag_GCC(version, severity, string)
241 #endif
242 
243 #define __diag_push()	__diag(push)
244 #define __diag_pop()	__diag(pop)
245 
246 #define __diag_ignore(compiler, version, option, comment) \
247 	__diag_ ## compiler(version, ignore, option)
248 #define __diag_warn(compiler, version, option, comment) \
249 	__diag_ ## compiler(version, warn, option)
250 #define __diag_error(compiler, version, option, comment) \
251 	__diag_ ## compiler(version, error, option)
252 
253 #endif /* __LINUX_COMPILER_TYPES_H */
254