2020-04-02 23:48:27 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
2006-08-23 16:28:37 +02:00
|
|
|
|
2010-02-28 19:13:09 +01:00
|
|
|
#ifndef __ASSERT_H__
|
|
|
|
#define __ASSERT_H__
|
2010-02-16 00:10:19 +01:00
|
|
|
|
2015-02-18 02:27:23 +01:00
|
|
|
#include <arch/hlt.h>
|
2014-02-21 09:22:52 +01:00
|
|
|
#include <console/console.h>
|
|
|
|
|
2019-05-09 23:12:17 +02:00
|
|
|
/* TODO: Fix vendorcode headers to not define macros coreboot uses or to be more
|
|
|
|
properly isolated. */
|
|
|
|
#ifdef ASSERT
|
|
|
|
#undef ASSERT
|
|
|
|
#endif
|
|
|
|
|
2020-06-08 15:52:03 +02:00
|
|
|
/* Do not use filenames nor line numbers on timeless builds, to preserve reproducibility */
|
|
|
|
#if ENV_TIMELESS
|
|
|
|
#define __ASSERT_FILE__ "(filenames not available on timeless builds)"
|
|
|
|
#define __ASSERT_LINE__ 404
|
|
|
|
#else
|
|
|
|
#define __ASSERT_FILE__ __FILE__
|
|
|
|
#define __ASSERT_LINE__ __LINE__
|
|
|
|
#endif
|
|
|
|
|
2010-02-28 19:13:09 +01:00
|
|
|
/* GCC and CAR versions */
|
2020-06-08 15:52:03 +02:00
|
|
|
#define ASSERT(x) { \
|
|
|
|
if (!(x)) { \
|
|
|
|
printk(BIOS_EMERG, \
|
|
|
|
"ASSERTION ERROR: file '%s', line %d\n", \
|
|
|
|
__ASSERT_FILE__, __ASSERT_LINE__); \
|
|
|
|
if (CONFIG(FATAL_ASSERTS)) \
|
|
|
|
hlt(); \
|
|
|
|
} \
|
2010-02-28 19:13:09 +01:00
|
|
|
}
|
2020-06-08 15:52:03 +02:00
|
|
|
#define ASSERT_MSG(x, msg) { \
|
|
|
|
if (!(x)) { \
|
|
|
|
printk(BIOS_EMERG, \
|
|
|
|
"ASSERTION ERROR: file '%s', line %d\n", \
|
|
|
|
__ASSERT_FILE__, __ASSERT_LINE__); \
|
|
|
|
printk(BIOS_EMERG, "%s", msg); \
|
|
|
|
if (CONFIG(FATAL_ASSERTS)) \
|
|
|
|
hlt(); \
|
|
|
|
} \
|
2019-12-17 18:02:15 +01:00
|
|
|
}
|
2020-06-08 15:52:03 +02:00
|
|
|
#define BUG() { \
|
|
|
|
printk(BIOS_EMERG, \
|
|
|
|
"ERROR: BUG ENCOUNTERED at file '%s', line %d\n", \
|
|
|
|
__ASSERT_FILE__, __ASSERT_LINE__); \
|
|
|
|
if (CONFIG(FATAL_ASSERTS)) \
|
|
|
|
hlt(); \
|
2010-02-28 19:13:09 +01:00
|
|
|
}
|
|
|
|
|
2014-02-21 09:22:52 +01:00
|
|
|
#define assert(statement) ASSERT(statement)
|
2010-03-01 18:19:55 +01:00
|
|
|
|
2017-07-14 23:14:11 +02:00
|
|
|
/*
|
|
|
|
* These macros can be used to assert that a certain branch of code is dead and
|
|
|
|
* will be compile-time eliminated. This differs from _Static_assert(), which
|
|
|
|
* will generate a compiler error even if the scope it was called from is dead
|
|
|
|
* code. This may be useful to double-check things like constants that are only
|
|
|
|
* valid if a certain Kconfig option is set.
|
2019-03-28 21:39:06 +01:00
|
|
|
*
|
|
|
|
* The error message when this hits will look like this:
|
|
|
|
*
|
|
|
|
* ramstage/lib/bootmode.o: In function `display_init_required':
|
2020-04-06 23:02:12 +02:00
|
|
|
* bootmode.c:42: undefined reference to `_dead_code_assertion_failed'
|
2017-07-14 23:14:11 +02:00
|
|
|
*/
|
2020-04-06 23:02:12 +02:00
|
|
|
extern void _dead_code_assertion_failed(void) __attribute__((noreturn));
|
|
|
|
#define dead_code() _dead_code_assertion_failed()
|
2017-07-14 23:14:11 +02:00
|
|
|
|
|
|
|
/* This can be used in the context of an expression of type 'type'. */
|
2019-03-28 21:39:06 +01:00
|
|
|
#define dead_code_t(type) ({ \
|
|
|
|
dead_code(); \
|
2017-07-14 23:14:11 +02:00
|
|
|
*(type *)(uintptr_t)0; \
|
|
|
|
})
|
|
|
|
|
2010-03-01 18:19:55 +01:00
|
|
|
#endif // __ASSERT_H__
|