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)
106 #define ALIGN(x) .align x
107 #else
108 #error Architecture unsupported
109 #endif
110
111#endif /* defined(_ASMLANGUAGE) && !defined(_LINKER) */
112
113/*
114 * If the project is being built for speed (i.e. not for minimum size) then
115 * align functions and branches in executable sections to improve performance.
116 */
117
118#ifdef _ASMLANGUAGE
119
120 #if defined(CONFIG_X86)
121
122 #ifdef PERF_OPT
123 #define PERFOPT_ALIGN .balign 16
124 #else
125 #define PERFOPT_ALIGN .balign 1
126 #endif
127
128 #elif defined(CONFIG_ARM) || defined(CONFIG_ARM64)
129
130 #define PERFOPT_ALIGN .balign 4
131
132 #elif defined(CONFIG_ARC)
133
134 /* .align assembler directive is supposed by all ARC toolchains and it is
135 * implemented in a same way across ARC toolchains.
136 */
137 #define PERFOPT_ALIGN .align 4
138
139 #elif defined(CONFIG_RISCV) || defined(CONFIG_XTENSA) || \
140 defined(CONFIG_MIPS) || defined(CONFIG_RX) || \
141 defined(CONFIG_OPENRISC)
142 #define PERFOPT_ALIGN .balign 4
143
144 #elif defined(CONFIG_ARCH_POSIX)
145
146 #elif defined(CONFIG_SPARC)
147
148 #define PERFOPT_ALIGN .align 4
149
150 #else
151
152 #error Architecture unsupported
153
154 #endif
155
156 #define GC_SECTION(sym) SECTION .text.##sym, "ax"
157
158#endif /* _ASMLANGUAGE */
159
160/* force inlining a function */
161
162#if !defined(_ASMLANGUAGE)
163 #ifdef CONFIG_COVERAGE
164 /*
165 * The always_inline attribute forces a function to be inlined,
166 * even ignoring -fno-inline. So for code coverage, do not
167 * force inlining of these functions to keep their bodies around
168 * so their number of executions can be counted.
169 *
170 * Note that "inline" is kept here for kobject_hash.c and
171 * priv_stacks_hash.c. These are built without compiler flags
172 * used for coverage. ALWAYS_INLINE cannot be empty as compiler
173 * would complain about unused functions. Attaching unused
174 * attribute would result in their text sections balloon more than
175 * 10 times in size, as those functions are kept in text section.
176 * So just keep "inline" here.
177 */
178 #define ALWAYS_INLINE inline
179 #else
180 #define ALWAYS_INLINE inline __attribute__((always_inline))
181 #endif
182#endif
183
184#define Z_STRINGIFY(x) #x
185#define STRINGIFY(s) Z_STRINGIFY(s)
186
187/* concatenate the values of the arguments into one */
188#define _DO_CONCAT(x, y) x ## y
189#define _CONCAT(x, y) _DO_CONCAT(x, y)
190
191/* Additionally used as a sentinel by gen_syscalls.py to identify what
192 * functions are system calls
193 *
194 * Note POSIX unit tests don't still generate the system call stubs, so
195 * until https://github.com/zephyrproject-rtos/zephyr/issues/5006 is
196 * fixed via possibly #4174, we introduce this hack -- which will
197 * disallow us to test system calls in POSIX unit testing (currently
198 * not used).
199 */
200#ifndef ZTEST_UNITTEST
201#define __syscall static inline
202#define __syscall_always_inline static inline __attribute__((always_inline))
203#else
204#define __syscall
205#define __syscall_always_inline
206#endif /* ZTEST_UNITTEST */
207
208/* Definitions for struct declaration tags. These are sentinel values used by
209 * parse_syscalls.py to gather a list of names of struct declarations that
210 * have these tags applied for them.
211 */
212
213/* Indicates this is a driver subsystem */
214#define __subsystem
215
216/* Indicates this is a network socket object */
217#define __net_socket
218
219#ifndef BUILD_ASSERT
220/* Compile-time assertion that makes the build to fail.
221 * Common implementation swallows the message.
222 */
223#define BUILD_ASSERT(EXPR, MSG...) \
224 enum _CONCAT(__build_assert_enum, __COUNTER__) { \
225 _CONCAT(__build_assert, __COUNTER__) = 1 / !!(EXPR) \
226 }
227#endif
228
229/*
230 * This is meant to be used in conjunction with __in_section() and similar
231 * where scattered structure instances are concatenated together by the linker
232 * and walked by the code at run time just like a contiguous array of such
233 * structures.
234 *
235 * Assemblers and linkers may insert alignment padding by default whose
236 * size is larger than the natural alignment for those structures when
237 * gathering various section segments together, messing up the array walk.
238 * To prevent this, we need to provide an explicit alignment not to rely
239 * on the default that might just work by luck.
240 *
241 * Alignment statements in linker scripts are not sufficient as
242 * the assembler may add padding by itself to each segment when switching
243 * between sections within the same file even if it merges many such segments
244 * into a single section in the end.
245 */
246#define Z_DECL_ALIGN(type) __aligned(__alignof(type)) type
247
248/* Check if a pointer is aligned for against a specific byte boundary */
249#define IS_PTR_ALIGNED_BYTES(ptr, bytes) ((((uintptr_t)ptr) % bytes) == 0)
250
251/* Check if a pointer is aligned enough for a particular data type. */
252#define IS_PTR_ALIGNED(ptr, type) IS_PTR_ALIGNED_BYTES(ptr, __alignof(type))
253
261#define LINKER_KEEP(symbol) \
262 static const void * const symbol##_ptr __used \
263 __attribute__((__section__(".symbol_to_keep"))) = (void *)&symbol
264
265#endif /* ZEPHYR_INCLUDE_TOOLCHAIN_COMMON_H_ */