| 1 |
|
| 2 |
|
| 3 |
#ifndef __ASSERT_H__ |
| 4 |
#define __ASSERT_H__ |
| 5 |
|
| 6 |
#include <arch/hlt.h> |
| 7 |
#include <console/console.h> |
| 8 |
#include <stdint.h> |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#ifdef ASSERT |
| 13 |
#undef ASSERT |
| 14 |
#endif |
| 15 |
|
| 16 |
|
| 17 |
#if ENV_TIMELESS |
| 18 |
#define __ASSERT_FILE__ "(filenames not available on timeless builds)" |
| 19 |
#define __ASSERT_LINE__ 404 |
| 20 |
#else |
| 21 |
#define __ASSERT_FILE__ __FILE__ |
| 22 |
#define __ASSERT_LINE__ __LINE__ |
| 23 |
#endif |
| 24 |
|
| 25 |
#ifndef _PORTING_H_ |
| 26 |
#define __build_time_assert(x) \ |
| 27 |
(__builtin_constant_p(x) ? ((x) ? 1 : dead_code_t(int)) : 0) |
| 28 |
#else |
| 29 |
#define __build_time_assert(x) 0 |
| 30 |
#endif |
| 31 |
|
| 32 |
|
| 33 |
void mock_assert(const int result, const char *const expression, |
| 34 |
const char *const file, const int line); |
| 35 |
|
| 36 |
#if ENV_TEST |
| 37 |
#define MOCK_ASSERT(result, expression) \ |
| 38 |
mock_assert((result), (expression), __ASSERT_FILE__, __ASSERT_LINE__) |
| 39 |
#else |
| 40 |
#define MOCK_ASSERT(result, expression) |
| 41 |
#endif |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
#define ASSERT(x) { \ |
| 56 |
if (!__build_time_assert(x) && !(x)) { \ |
| 57 |
printk(BIOS_EMERG, \ |
| 58 |
"ASSERTION ERROR: file '%s', line %d\n", \ |
| 59 |
__ASSERT_FILE__, __ASSERT_LINE__); \ |
| 60 |
MOCK_ASSERT(!!(x), #x); \ |
| 61 |
if (CONFIG(FATAL_ASSERTS)) \ |
| 62 |
hlt(); \ |
| 63 |
} \ |
| 64 |
} |
| 65 |
#define ASSERT_MSG(x, msg) { \ |
| 66 |
if (!__build_time_assert(x) && !(x)) { \ |
| 67 |
printk(BIOS_EMERG, \ |
| 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)) \ |
| 73 |
hlt(); \ |
| 74 |
} \ |
| 75 |
} |
| 76 |
#define BUG() { \ |
| 77 |
printk(BIOS_EMERG, \ |
| 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)) \ |
| 82 |
hlt(); \ |
| 83 |
} |
| 84 |
|
| 85 |
#define assert(statement) ASSERT(statement) |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
void _dead_code_assertion_failed(void) __attribute__((noreturn)); |
| 100 |
#define dead_code() _dead_code_assertion_failed() |
| 101 |
|
| 102 |
|
| 103 |
#define dead_code_t(type) ({ \ |
| 104 |
dead_code(); \ |
| 105 |
*(type *)(uintptr_t)0; \ |
| 106 |
}) |
| 107 |
|
| 108 |
#if ENV_X86_64 |
| 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); \ |
| 113 |
}) |
| 114 |
#else |
| 115 |
#define pointer_to_uint32_safe(x) ({ \ |
| 116 |
(uint32_t)(uintptr_t)(x); \ |
| 117 |
}) |
| 118 |
#endif |
| 119 |
#endif |