1 /*
2  * Copyright (c) 2016-2025 Nordic Semiconductor ASA
3  * Copyright (c) 2016 Vinayak Kariappa Chettimada
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include "common/assert.h"
9 
10 #if defined(CONFIG_BT_CTLR_ASSERT_HANDLER)
11 void bt_ctlr_assert_handle(char *file, uint32_t line);
12 
13 #if defined(CONFIG_BT_CTLR_ASSERT_OPTIMIZE_FOR_SIZE)
14 BUILD_ASSERT(IS_ENABLED(CONFIG_CPU_CORTEX_M));
15 /* Generate assertion as undefined instruction exception.
16  */
17 #define LL_ASSERT(x) \
18 	do { \
19 		if (unlikely(!(x))) { \
20 			__asm__ inline volatile (".inst 0xde00\n"); \
21 		} \
22 	} while (0)
23 
24 #else /* !CONFIG_BT_CTLR_ASSERT_OPTIMIZE_FOR_SIZE */
25 /* Generate assertion with file name and line number.
26  * NOTE: Variable code size increase per assertion check, depends on full file name path string
27  *       length.
28  */
29 #define LL_ASSERT(cond) \
30 		if (unlikely(!(cond))) { \
31 			BT_ASSERT_PRINT(cond); \
32 			bt_ctlr_assert_handle(__FILE__, __LINE__); \
33 		}
34 #endif /* !CONFIG_BT_CTLR_ASSERT_OPTIMIZE_FOR_SIZE */
35 
36 #define LL_ASSERT_MSG(cond, fmt, ...) \
37 		if (unlikely(!(cond))) { \
38 			BT_ASSERT_PRINT(cond); \
39 			BT_ASSERT_PRINT_MSG(fmt, ##__VA_ARGS__); \
40 			bt_ctlr_assert_handle(__FILE__, __LINE__); \
41 		}
42 
43 #else /* !CONFIG_BT_CTLR_ASSERT_HANDLER */
44 
45 #if defined(CONFIG_BT_CTLR_ASSERT_OPTIMIZE_FOR_SIZE)
46 BUILD_ASSERT(IS_ENABLED(CONFIG_CPU_CORTEX_M));
47 /* Generate assertion as undefined instruction exception.
48  */
49 #define LL_ASSERT(x) \
50 	do { \
51 		if (unlikely(!(x))) { \
52 			__asm__ inline volatile (".inst 0xde00\n"); \
53 		} \
54 	} while (0)
55 
56 #else /* !CONFIG_BT_CTLR_ASSERT_OPTIMIZE_FOR_SIZE */
57 #define LL_ASSERT(cond) \
58 		BT_ASSERT(cond)
59 #endif /* !CONFIG_BT_CTLR_ASSERT_OPTIMIZE_FOR_SIZE */
60 
61 #define LL_ASSERT_MSG(cond, fmt, ...) \
62 		BT_ASSERT_MSG(cond, fmt, ##__VA_ARGS__)
63 #endif /* !CONFIG_BT_CTLR_ASSERT_HANDLER */
64 
65 /* Fatal asserts.
66  * The Controller will otherwise misbehave causing memory leak or system-wide memory corruptions due
67  * to uncontrolled DMA transfers etc.
68  * It is not safe to disable these assertion checks.
69  */
70 #define LL_ASSERT_ERR(cond) LL_ASSERT(cond)
71 
72 /* Development asserts.
73  * The Controller will continue to function without memory leak or corruption with these assertion
74  * checks disabled. Example, run-time mis-aligned memory access etc. which do otherwise implicitly
75  * cause CPU fault during development testing. But these type of asserted are essentially required
76  * for debugging, code and unit test coverage during development cycle.
77  */
78 #if defined(CONFIG_BT_CTLR_ASSERT_DEBUG)
79 #define LL_ASSERT_DBG(cond) LL_ASSERT(cond)
80 #else /* !CONFIG_BT_CTLR_ASSERT_DEBUG */
81 #define LL_ASSERT_DBG(cond) ARG_UNUSED((cond))
82 #endif /* !CONFIG_BT_CTLR_ASSERT_DEBUG */
83 
84 #if defined(CONFIG_BT_CTLR_ASSERT_VENDOR)
85 #define LL_ASSERT_INFO1(cond, param) \
86 		BT_ASSERT_VND(cond, param, 0)
87 #define LL_ASSERT_INFO2(cond, param1, param2) \
88 		BT_ASSERT_VND(cond, param1, param2)
89 #else
90 #define LL_ASSERT_INFO1(cond, param) \
91 		LL_ASSERT_MSG(cond, "param: %u", param)
92 #define LL_ASSERT_INFO2(cond, param1, param2) \
93 		LL_ASSERT_MSG(cond, "param1: %u param2: %u", param1, param2)
94 #endif /* CONFIG_BT_CTLR_ASSERT_VENDOR */
95 
96 #if defined(CONFIG_BT_CTLR_ASSERT_OVERHEAD_START)
97 #define LL_ASSERT_OVERHEAD(overhead) \
98 	LL_ASSERT_MSG(false, "%s: Actual EVENT_OVERHEAD_START_US = %u", \
99 		      __func__, HAL_TICKER_TICKS_TO_US(overhead));
100 #else /* !CONFIG_BT_CTLR_ASSERT_OVERHEAD_START */
101 #define LL_ASSERT_OVERHEAD(overhead) ARG_UNUSED(overhead)
102 #endif /* !CONFIG_BT_CTLR_ASSERT_OVERHEAD_START */
103 
104 #include "hal/debug_vendor_hal.h"
105