1 /* SPDX-License-Identifier: GPL-2.0-only */
7 #include <console/console.h>
10 /* TODO: Fix vendorcode headers to not define macros coreboot uses or to be more
16 /* Do not use filenames nor line numbers on timeless builds, to preserve reproducibility */
18 #define __ASSERT_FILE__ "(filenames not available on timeless builds)"
19 #define __ASSERT_LINE__ 404
21 #define __ASSERT_FILE__ __FILE__
22 #define __ASSERT_LINE__ __LINE__
25 #ifndef _PORTING_H_ /* TODO: Isolate AGESA properly. */
26 #define __build_time_assert(x) \
27 (__builtin_constant_p(x) ? ((x) ? 1 : dead_code_t(int)) : 0)
29 #define __build_time_assert(x) 0
32 /* CMocka function redefinition. */
33 void mock_assert(const int result
, const char *const expression
,
34 const char *const file
, const int line
);
37 #define MOCK_ASSERT(result, expression) \
38 mock_assert((result), (expression), __ASSERT_FILE__, __ASSERT_LINE__)
40 #define MOCK_ASSERT(result, expression)
44 * assert() should be used to test stuff that the programmer *knows* to be true.
45 * It should not be used to test something that may actually change at runtime
46 * (e.g. anything involving hardware accesses). For example, testing whether
47 * function parameters match the documented requirements is a good use of
48 * assert() (where it is still the responsibility of the caller to ensure it
49 * passes valid values, and the callee is just double-checking).
51 * Depending on CONFIG(FATAL_ASSERTS), assert() will either halt execution or
52 * just print an error message and continue. For more guidelines on error
53 * handling, see Documentation/contributing/coding_style.md.
56 if (!__build_time_assert(x) && !(x)) { \
58 "ASSERTION ERROR: file '%s', line %d\n", \
59 __ASSERT_FILE__, __ASSERT_LINE__); \
60 MOCK_ASSERT(!!(x), #x); \
61 if (CONFIG(FATAL_ASSERTS)) \
65 #define ASSERT_MSG(x, msg) { \
66 if (!__build_time_assert(x) && !(x)) { \
68 "ASSERTION ERROR: file '%s', line %d\n", \
69 __ASSERT_FILE__, __ASSERT_LINE__); \
70 printk(BIOS_EMERG, "%s", msg); \
71 MOCK_ASSERT(!!(x), (msg)); \
72 if (CONFIG(FATAL_ASSERTS)) \
78 "ERROR: BUG ENCOUNTERED at file '%s', line %d\n", \
79 __ASSERT_FILE__, __ASSERT_LINE__); \
80 MOCK_ASSERT(0, "BUG ENCOUNTERED"); \
81 if (CONFIG(FATAL_ASSERTS)) \
85 #define assert(statement) ASSERT(statement)
88 * These macros can be used to assert that a certain branch of code is dead and
89 * will be compile-time eliminated. This differs from _Static_assert(), which
90 * will generate a compiler error even if the scope it was called from is dead
91 * code. This may be useful to double-check things like constants that are only
92 * valid if a certain Kconfig option is set.
94 * The error message when this hits will look like this:
96 * ramstage/lib/bootmode.o: In function `display_init_required':
97 * bootmode.c:42: undefined reference to `_dead_code_assertion_failed'
99 void _dead_code_assertion_failed(void) __attribute__((noreturn
));
100 #define dead_code() _dead_code_assertion_failed()
102 /* This can be used in the context of an expression of type 'type'. */
103 #define dead_code_t(type) ({ \
105 *(type *)(uintptr_t)0; \
109 #define pointer_to_uint32_safe(x) ({ \
110 if ((uintptr_t)(x) > 0xffffffffUL) \
111 die("Cast from pointer to uint32_t overflows"); \
112 (uint32_t)(uintptr_t)(x); \
115 #define pointer_to_uint32_safe(x) ({ \
116 (uint32_t)(uintptr_t)(x); \
119 #endif // __ASSERT_H__