coreboot-kgpe-d16/src/include/assert.h

95 lines
2.7 KiB
C
Raw Normal View History

/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef __ASSERT_H__
#define __ASSERT_H__
Fix non-x86 __PRE_RAM__ assertions and add FATAL_ASSERTS Kconfig option This patch fixes a bug that caused non-x86 boards to use the poor man's assert() version with a lot more instructions per invocation and hexadecimal line numbers in __PRE_RAM__ environments. This was really just an oversight in the ARM port... even x86 uses a proper printk() in most cases (those with CAR) and there's no reason not to do so on the generally even more flexible SRAM-based architectures. Additionally, it adds a new Kconfig option to make failed assertions and BUG() calls halt again. This seems to have been the original intention, but was commented out once out of fear that this might prevent production systems from booting. It is still a useful debugging feature though (since otherwise assertions can easily just scroll past and get overlooked), so the user should be able to decide the this based on his needs. (Also changed error messages for both to include the word "ERROR", since grepping for that is the most sophisticated way we currently have to detect firmware problems. Some automated Chromium OS suspend tests check for that.) BRANCH=veyron BUG=None TEST=Booted Jerry. Compared binary sizes before and after, new version's bootblock is some ~600 bytes smaller. Change-Id: I894da18d77e12bf104e443322e2d58e60564e4b7 Signed-off-by: Patrick Georgi <pgeorgi@chromium.org> Original-Commit-Id: 6a5343124719c18a1c969477e3d18bda13c0bf26 Original-Change-Id: I0268cfd67d8c894406b18bb3759a577944bcffb1 Original-Signed-off-by: Julius Werner <jwerner@chromium.org> Original-Reviewed-on: https://chromium-review.googlesource.com/250661 Original-Reviewed-by: Vadim Bendebury <vbendeb@chromium.org> Original-Reviewed-by: Aaron Durbin <adurbin@chromium.org> Reviewed-on: http://review.coreboot.org/9775 Tested-by: build bot (Jenkins) Reviewed-by: Edward O'Callaghan <edward.ocallaghan@koparo.com>
2015-02-18 02:27:23 +01:00
#include <arch/hlt.h>
#include <console/console.h>
#include <stdint.h>
/* TODO: Fix vendorcode headers to not define macros coreboot uses or to be more
properly isolated. */
#ifdef ASSERT
#undef ASSERT
#endif
/* 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
#ifndef _PORTING_H_ /* TODO: Isolate AGESA properly. */
#define __build_time_assert(x) \
(__builtin_constant_p(x) ? ((x) ? 1 : dead_code_t(int)) : 0)
#else
#define __build_time_assert(x) 0
#endif
/* GCC and CAR versions */
#define ASSERT(x) { \
if (!__build_time_assert(x) && !(x)) { \
printk(BIOS_EMERG, \
"ASSERTION ERROR: file '%s', line %d\n", \
__ASSERT_FILE__, __ASSERT_LINE__); \
if (CONFIG(FATAL_ASSERTS)) \
hlt(); \
} \
}
#define ASSERT_MSG(x, msg) { \
if (!__build_time_assert(x) && !(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(); \
} \
}
#define BUG() { \
printk(BIOS_EMERG, \
"ERROR: BUG ENCOUNTERED at file '%s', line %d\n", \
__ASSERT_FILE__, __ASSERT_LINE__); \
if (CONFIG(FATAL_ASSERTS)) \
hlt(); \
}
#define assert(statement) ASSERT(statement)
/*
* 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.
*
* The error message when this hits will look like this:
*
* ramstage/lib/bootmode.o: In function `display_init_required':
* bootmode.c:42: undefined reference to `_dead_code_assertion_failed'
*/
extern void _dead_code_assertion_failed(void) __attribute__((noreturn));
#define dead_code() _dead_code_assertion_failed()
/* This can be used in the context of an expression of type 'type'. */
#define dead_code_t(type) ({ \
dead_code(); \
*(type *)(uintptr_t)0; \
})
#ifdef __x86_64__
#define pointer_to_uint32_safe(x) ({ \
if ((uintptr_t)(x) > 0xffffffffUL) \
die("Cast from pointer to uint32_t overflows"); \
(uint32_t)(uintptr_t)(x); \
})
#else
#define pointer_to_uint32_safe(x) ({ \
(uint32_t)(uintptr_t)(x); \
})
#endif
#endif // __ASSERT_H__