Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
common.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010-2014 Wind River Systems, Inc.
3 * Copyright (c) 2025 Siemens AG
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef ZEPHYR_INCLUDE_TOOLCHAIN_COMMON_H_
9#define ZEPHYR_INCLUDE_TOOLCHAIN_COMMON_H_
10
11#ifndef ZEPHYR_INCLUDE_TOOLCHAIN_H_
12#error Please do not include toolchain-specific headers directly, use <zephyr/toolchain.h> instead
13#endif
14
21
22/* Abstract use of extern keyword for compatibility between C and C++ */
23#ifdef __cplusplus
24#define EXTERN_C extern "C"
25#else
26#define EXTERN_C extern
27#endif
28
29/* Use TASK_ENTRY_CPP to tag task entry points defined in C++ files. */
30
31#ifdef __cplusplus
32#define TASK_ENTRY_CPP extern "C"
33#endif
34
35#ifndef ZRESTRICT
36#ifndef __cplusplus
37#define ZRESTRICT restrict
38#else
39#define ZRESTRICT
40#endif
41#endif
42
43/*
44 * Generate a reference to an external symbol.
45 * The reference indicates to the linker that the symbol is required
46 * by the module containing the reference and should be included
47 * in the image if the module is in the image.
48 *
49 * The assembler directive ".set" is used to define a local symbol.
50 * No memory is allocated, and the local symbol does not appear in
51 * the symbol table.
52 */
53
54#ifdef _ASMLANGUAGE
55 #define REQUIRES(sym) .set sym ## _Requires, sym
56#else
57 #define REQUIRES(sym) __asm__ (".set " # sym "_Requires, " # sym "\n\t");
58#endif
59
60#ifdef _ASMLANGUAGE
61 #define SECTION .section
62#endif
63
64/*
65 * Assembler directives to emit into a section other than the current one and
66 * then restore it, for use from inline assembly.
67 *
68 * Example:
69 *
70 * __asm__(PUSHSECTION_DIRECTIVE " .my_section,\"\"\n\t"
71 * ".asciz \"payload\"\n\t"
72 * POPSECTION_DIRECTIVE);
73 */
74#if defined(CONFIG_ARC) && defined(__CCAC__)
75/* The ARC MWDT assembler spells these differently. */
76 #define PUSHSECTION_DIRECTIVE ".pushsect"
77 #define POPSECTION_DIRECTIVE ".popsect"
78#else
79 #define PUSHSECTION_DIRECTIVE ".pushsection"
80 #define POPSECTION_DIRECTIVE ".popsection"
81#endif
82
83/*
84 * General directive for assembly code, to align the following symbol, in bytes.
85 *
86 * Example:
87 *
88 * ALIGN(4)
89 * test_symbol:
90 *
91 * 'test_symbol' will get aligned to 4 bytes.
92 */
93
94#if defined(_ASMLANGUAGE) && !defined(_LINKER)
95
96 #if defined(CONFIG_X86) || defined(CONFIG_ARM) || defined(CONFIG_ARM64) || \
97 defined(CONFIG_RISCV) || defined(CONFIG_XTENSA) || defined(CONFIG_MIPS) || \
98 defined(CONFIG_ARCH_POSIX) || defined(CONFIG_RX) || defined(CONFIG_OPENRISC)
99 #define ALIGN(x) .balign x
100 #elif defined(CONFIG_ARC)
101 /* .align assembler directive is supported by all ARC toolchains and it is
102 * implemented in the same way across ARC toolchains.
103 */
104 #define ALIGN(x) .align x
105 #elif defined(CONFIG_SPARC) || defined(CONFIG_TRICORE)
106 #define ALIGN(x) .align x
107 #elif defined(CONFIG_HEXAGON)
108 #define ALIGN(x) .balign x
109 #else
110 #error Architecture unsupported
111 #endif
112
113#endif /* defined(_ASMLANGUAGE) && !defined(_LINKER) */
114
115/*
116 * If the project is being built for speed (i.e. not for minimum size) then
117 * align functions and branches in executable sections to improve performance.
118 */
119
120#ifdef _ASMLANGUAGE
121
122 #if defined(CONFIG_X86)
123
124 #ifdef PERF_OPT
125 #define PERFOPT_ALIGN .balign 16
126 #else
127 #define PERFOPT_ALIGN .balign 1
128 #endif
129
130 #elif defined(CONFIG_ARM) || defined(CONFIG_ARM64)
131
132 #define PERFOPT_ALIGN .balign 4
133
134 #elif defined(CONFIG_ARC)
135
136 /* .align assembler directive is supposed by all ARC toolchains and it is
137 * implemented in a same way across ARC toolchains.
138 */
139 #define PERFOPT_ALIGN .align 4
140
141 #elif defined(CONFIG_RISCV) || defined(CONFIG_XTENSA) || \
142 defined(CONFIG_MIPS) || defined(CONFIG_RX) || \
143 defined(CONFIG_OPENRISC)
144 #define PERFOPT_ALIGN .balign 4
145
146 #elif defined(CONFIG_ARCH_POSIX)
147
148 #elif defined(CONFIG_SPARC) || defined(CONFIG_TRICORE)
149
150 #define PERFOPT_ALIGN .align 4
151
152 #elif defined(CONFIG_HEXAGON)
153
154 #define PERFOPT_ALIGN .balign 4
155
156 #else
157
158 #error Architecture unsupported
159
160 #endif
161
162 #define GC_SECTION(sym) SECTION .text.##sym, "ax"
163
164#endif /* _ASMLANGUAGE */
165
166/* force inlining a function */
167
168#if !defined(_ASMLANGUAGE)
169 #ifdef CONFIG_COVERAGE
170 /*
171 * The always_inline attribute forces a function to be inlined,
172 * even ignoring -fno-inline. So for code coverage, do not
173 * force inlining of these functions to keep their bodies around
174 * so their number of executions can be counted.
175 *
176 * Note that "inline" is kept here for kobject_hash.c and
177 * priv_stacks_hash.c. These are built without compiler flags
178 * used for coverage. ALWAYS_INLINE cannot be empty as compiler
179 * would complain about unused functions. Attaching unused
180 * attribute would result in their text sections balloon more than
181 * 10 times in size, as those functions are kept in text section.
182 * So just keep "inline" here.
183 */
184 #define ALWAYS_INLINE inline
185 #else
186 #define ALWAYS_INLINE inline __attribute__((always_inline))
187 #endif
188#endif
189
190#define Z_STRINGIFY(x) #x
191#define STRINGIFY(s) Z_STRINGIFY(s)
192
193/* concatenate the values of the arguments into one */
194#define _DO_CONCAT(x, y) x ## y
195#define _CONCAT(x, y) _DO_CONCAT(x, y)
196
197/* Additionally used as a sentinel by gen_syscalls.py to identify what
198 * functions are system calls
199 *
200 * Note POSIX unit tests don't still generate the system call stubs, so
201 * until https://github.com/zephyrproject-rtos/zephyr/issues/5006 is
202 * fixed via possibly #4174, we introduce this hack -- which will
203 * disallow us to test system calls in POSIX unit testing (currently
204 * not used).
205 */
206#ifndef ZTEST_UNITTEST
207#define __syscall static inline
208#define __syscall_always_inline static inline __attribute__((always_inline))
209#else
210#define __syscall
211#define __syscall_always_inline
212#endif /* ZTEST_UNITTEST */
213
214/* Definitions for struct declaration tags. These are sentinel values used by
215 * parse_syscalls.py to gather a list of names of struct declarations that
216 * have these tags applied for them.
217 */
218
219/* Indicates this is a driver subsystem */
220#define __subsystem
221
222/* Indicates this is a network socket object */
223#define __net_socket
224
225#ifndef BUILD_ASSERT
226/* Compile-time assertion that makes the build to fail.
227 * Common implementation swallows the message.
228 */
229#define BUILD_ASSERT(EXPR, MSG...) \
230 enum _CONCAT(__build_assert_enum, __COUNTER__) { \
231 _CONCAT(__build_assert, __COUNTER__) = 1 / !!(EXPR) \
232 }
233#endif
234
235/*
236 * This is meant to be used in conjunction with __in_section() and similar
237 * where scattered structure instances are concatenated together by the linker
238 * and walked by the code at run time just like a contiguous array of such
239 * structures.
240 *
241 * Assemblers and linkers may insert alignment padding by default whose
242 * size is larger than the natural alignment for those structures when
243 * gathering various section segments together, messing up the array walk.
244 * To prevent this, we need to provide an explicit alignment not to rely
245 * on the default that might just work by luck.
246 *
247 * Alignment statements in linker scripts are not sufficient as
248 * the assembler may add padding by itself to each segment when switching
249 * between sections within the same file even if it merges many such segments
250 * into a single section in the end.
251 */
252#define Z_DECL_ALIGN(type) __aligned(__alignof(type)) type
253
254/* Check if a pointer is aligned for against a specific byte boundary */
255#define IS_PTR_ALIGNED_BYTES(ptr, bytes) ((((uintptr_t)ptr) % bytes) == 0)
256
257/* Check if a pointer is aligned enough for a particular data type. */
258#define IS_PTR_ALIGNED(ptr, type) IS_PTR_ALIGNED_BYTES(ptr, __alignof(type))
259
267#define LINKER_KEEP(symbol) \
268 static const void * const symbol##_ptr __used \
269 __attribute__((__section__(".symbol_to_keep"))) = (void *)&symbol
270
271#endif /* ZEPHYR_INCLUDE_TOOLCHAIN_COMMON_H_ */