xref: /openbmc/linux/include/linux/bug.h (revision ca623c91)
17664c5a1SJeremy Fitzhardinge #ifndef _LINUX_BUG_H
27664c5a1SJeremy Fitzhardinge #define _LINUX_BUG_H
37664c5a1SJeremy Fitzhardinge 
47664c5a1SJeremy Fitzhardinge #include <asm/bug.h>
57664c5a1SJeremy Fitzhardinge 
67664c5a1SJeremy Fitzhardinge enum bug_trap_type {
77664c5a1SJeremy Fitzhardinge 	BUG_TRAP_TYPE_NONE = 0,
87664c5a1SJeremy Fitzhardinge 	BUG_TRAP_TYPE_WARN = 1,
97664c5a1SJeremy Fitzhardinge 	BUG_TRAP_TYPE_BUG = 2,
107664c5a1SJeremy Fitzhardinge };
117664c5a1SJeremy Fitzhardinge 
12608e2619SHeiko Carstens struct pt_regs;
13608e2619SHeiko Carstens 
1435edd910SPaul Gortmaker #ifdef __CHECKER__
15ca623c91SDaniel Santos #define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0)
1635edd910SPaul Gortmaker #define BUILD_BUG_ON_ZERO(e) (0)
1735edd910SPaul Gortmaker #define BUILD_BUG_ON_NULL(e) ((void*)0)
18c5782e9fSTushar Behera #define BUILD_BUG_ON_INVALID(e) (0)
19ca623c91SDaniel Santos #define BUILD_BUG_ON(condition) (0)
2035edd910SPaul Gortmaker #define BUILD_BUG() (0)
2135edd910SPaul Gortmaker #else /* __CHECKER__ */
2235edd910SPaul Gortmaker 
2335edd910SPaul Gortmaker /* Force a compilation error if a constant expression is not a power of 2 */
2435edd910SPaul Gortmaker #define BUILD_BUG_ON_NOT_POWER_OF_2(n)			\
2535edd910SPaul Gortmaker 	BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
2635edd910SPaul Gortmaker 
2735edd910SPaul Gortmaker /* Force a compilation error if condition is true, but also produce a
2835edd910SPaul Gortmaker    result (of value 0 and type size_t), so the expression can be used
2935edd910SPaul Gortmaker    e.g. in a structure initializer (or where-ever else comma expressions
3035edd910SPaul Gortmaker    aren't permitted). */
3135edd910SPaul Gortmaker #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
3235edd910SPaul Gortmaker #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
3335edd910SPaul Gortmaker 
34baf05aa9SKonstantin Khlebnikov /*
35baf05aa9SKonstantin Khlebnikov  * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the
36baf05aa9SKonstantin Khlebnikov  * expression but avoids the generation of any code, even if that expression
37baf05aa9SKonstantin Khlebnikov  * has side-effects.
38baf05aa9SKonstantin Khlebnikov  */
39baf05aa9SKonstantin Khlebnikov #define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e))))
40baf05aa9SKonstantin Khlebnikov 
4135edd910SPaul Gortmaker /**
4235edd910SPaul Gortmaker  * BUILD_BUG_ON - break compile if a condition is true.
4335edd910SPaul Gortmaker  * @condition: the condition which the compiler should know is false.
4435edd910SPaul Gortmaker  *
4535edd910SPaul Gortmaker  * If you have some code which relies on certain constants being equal, or
4635edd910SPaul Gortmaker  * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
4735edd910SPaul Gortmaker  * detect if someone changes it.
4835edd910SPaul Gortmaker  *
4935edd910SPaul Gortmaker  * The implementation uses gcc's reluctance to create a negative array, but
5035edd910SPaul Gortmaker  * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
5135edd910SPaul Gortmaker  * to inline functions).  So as a fallback we use the optimizer; if it can't
5235edd910SPaul Gortmaker  * prove the condition is false, it will cause a link error on the undefined
5335edd910SPaul Gortmaker  * "__build_bug_on_failed".  This error message can be harder to track down
5435edd910SPaul Gortmaker  * though, hence the two different methods.
5535edd910SPaul Gortmaker  */
5635edd910SPaul Gortmaker #ifndef __OPTIMIZE__
5735edd910SPaul Gortmaker #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
5835edd910SPaul Gortmaker #else
5935edd910SPaul Gortmaker extern int __build_bug_on_failed;
6035edd910SPaul Gortmaker #define BUILD_BUG_ON(condition)					\
6135edd910SPaul Gortmaker 	do {							\
6235edd910SPaul Gortmaker 		((void)sizeof(char[1 - 2*!!(condition)]));	\
6335edd910SPaul Gortmaker 		if (condition) __build_bug_on_failed = 1;	\
6435edd910SPaul Gortmaker 	} while(0)
6535edd910SPaul Gortmaker #endif
6635edd910SPaul Gortmaker 
6735edd910SPaul Gortmaker /**
6835edd910SPaul Gortmaker  * BUILD_BUG - break compile if used.
6935edd910SPaul Gortmaker  *
7035edd910SPaul Gortmaker  * If you have some code that you expect the compiler to eliminate at
7135edd910SPaul Gortmaker  * build time, you should use BUILD_BUG to detect if it is
7235edd910SPaul Gortmaker  * unexpectedly used.
7335edd910SPaul Gortmaker  */
7435edd910SPaul Gortmaker #define BUILD_BUG()						\
7535edd910SPaul Gortmaker 	do {							\
7635edd910SPaul Gortmaker 		extern void __build_bug_failed(void)		\
776ae8d048SDaniel Santos 			__compiletime_error("BUILD_BUG failed");\
7835edd910SPaul Gortmaker 		__build_bug_failed();				\
7935edd910SPaul Gortmaker 	} while (0)
8035edd910SPaul Gortmaker 
8135edd910SPaul Gortmaker #endif	/* __CHECKER__ */
8235edd910SPaul Gortmaker 
837664c5a1SJeremy Fitzhardinge #ifdef CONFIG_GENERIC_BUG
847664c5a1SJeremy Fitzhardinge #include <asm-generic/bug.h>
857664c5a1SJeremy Fitzhardinge 
867664c5a1SJeremy Fitzhardinge static inline int is_warning_bug(const struct bug_entry *bug)
877664c5a1SJeremy Fitzhardinge {
887664c5a1SJeremy Fitzhardinge 	return bug->flags & BUGFLAG_WARNING;
897664c5a1SJeremy Fitzhardinge }
907664c5a1SJeremy Fitzhardinge 
917664c5a1SJeremy Fitzhardinge const struct bug_entry *find_bug(unsigned long bugaddr);
927664c5a1SJeremy Fitzhardinge 
93608e2619SHeiko Carstens enum bug_trap_type report_bug(unsigned long bug_addr, struct pt_regs *regs);
947664c5a1SJeremy Fitzhardinge 
957664c5a1SJeremy Fitzhardinge /* These are defined by the architecture */
967664c5a1SJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr);
977664c5a1SJeremy Fitzhardinge 
987664c5a1SJeremy Fitzhardinge #else	/* !CONFIG_GENERIC_BUG */
997664c5a1SJeremy Fitzhardinge 
100608e2619SHeiko Carstens static inline enum bug_trap_type report_bug(unsigned long bug_addr,
101608e2619SHeiko Carstens 					    struct pt_regs *regs)
1027664c5a1SJeremy Fitzhardinge {
1037664c5a1SJeremy Fitzhardinge 	return BUG_TRAP_TYPE_BUG;
1047664c5a1SJeremy Fitzhardinge }
1057664c5a1SJeremy Fitzhardinge 
1067664c5a1SJeremy Fitzhardinge #endif	/* CONFIG_GENERIC_BUG */
1077664c5a1SJeremy Fitzhardinge #endif	/* _LINUX_BUG_H */
108