blob: e51b0709e78dce274a915cf9fbd16c80f70b6b94 [file] [log] [blame]
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -08001#ifndef _LINUX_BUG_H
2#define _LINUX_BUG_H
3
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -08004#include <asm/bug.h>
Daniel Santosa3ccc492013-02-21 16:41:52 -08005#include <linux/compiler.h>
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -08006
7enum bug_trap_type {
8 BUG_TRAP_TYPE_NONE = 0,
9 BUG_TRAP_TYPE_WARN = 1,
10 BUG_TRAP_TYPE_BUG = 2,
11};
12
Heiko Carstens608e2612007-07-15 23:41:39 -070013struct pt_regs;
14
Paul Gortmaker35edd912011-11-16 23:51:05 -050015#ifdef __CHECKER__
Daniel Santosca623c92013-02-21 16:41:44 -080016#define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0)
Paul Gortmaker35edd912011-11-16 23:51:05 -050017#define BUILD_BUG_ON_ZERO(e) (0)
18#define BUILD_BUG_ON_NULL(e) ((void*)0)
Tushar Beherac5782e92012-11-26 16:29:38 -080019#define BUILD_BUG_ON_INVALID(e) (0)
Daniel Santos9a8ab1c2013-02-21 16:41:55 -080020#define BUILD_BUG_ON_MSG(cond, msg) (0)
Daniel Santosca623c92013-02-21 16:41:44 -080021#define BUILD_BUG_ON(condition) (0)
Paul Gortmaker35edd912011-11-16 23:51:05 -050022#define BUILD_BUG() (0)
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +053023#define MAYBE_BUILD_BUG_ON(cond) (0)
Paul Gortmaker35edd912011-11-16 23:51:05 -050024#else /* __CHECKER__ */
25
26/* Force a compilation error if a constant expression is not a power of 2 */
27#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
28 BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
29
30/* Force a compilation error if condition is true, but also produce a
31 result (of value 0 and type size_t), so the expression can be used
32 e.g. in a structure initializer (or where-ever else comma expressions
33 aren't permitted). */
34#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
35#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
36
Konstantin Khlebnikovbaf05aa2012-05-29 15:06:27 -070037/*
38 * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the
39 * expression but avoids the generation of any code, even if that expression
40 * has side-effects.
41 */
42#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e))))
43
Paul Gortmaker35edd912011-11-16 23:51:05 -050044/**
Daniel Santos9a8ab1c2013-02-21 16:41:55 -080045 * BUILD_BUG_ON_MSG - break compile if a condition is true & emit supplied
46 * error message.
47 * @condition: the condition which the compiler should know is false.
48 *
49 * See BUILD_BUG_ON for description.
50 */
51#define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
52
53/**
Paul Gortmaker35edd912011-11-16 23:51:05 -050054 * BUILD_BUG_ON - break compile if a condition is true.
55 * @condition: the condition which the compiler should know is false.
56 *
57 * If you have some code which relies on certain constants being equal, or
Daniel Santosa3ccc492013-02-21 16:41:52 -080058 * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to
Paul Gortmaker35edd912011-11-16 23:51:05 -050059 * detect if someone changes it.
60 *
Daniel Santosa3ccc492013-02-21 16:41:52 -080061 * The implementation uses gcc's reluctance to create a negative array, but gcc
62 * (as of 4.4) only emits that error for obvious cases (e.g. not arguments to
63 * inline functions). Luckily, in 4.3 they added the "error" function
64 * attribute just for this type of case. Thus, we use a negative sized array
65 * (should always create an error on gcc versions older than 4.4) and then call
66 * an undefined function with the error attribute (should always create an
67 * error on gcc 4.3 and later). If for some reason, neither creates a
68 * compile-time error, we'll still have a link-time error, which is harder to
69 * track down.
Paul Gortmaker35edd912011-11-16 23:51:05 -050070 */
71#ifndef __OPTIMIZE__
72#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
73#else
Daniel Santos9a8ab1c2013-02-21 16:41:55 -080074#define BUILD_BUG_ON(condition) \
75 BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
Paul Gortmaker35edd912011-11-16 23:51:05 -050076#endif
77
78/**
79 * BUILD_BUG - break compile if used.
80 *
81 * If you have some code that you expect the compiler to eliminate at
82 * build time, you should use BUILD_BUG to detect if it is
83 * unexpectedly used.
84 */
Daniel Santos9a8ab1c2013-02-21 16:41:55 -080085#define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed")
Paul Gortmaker35edd912011-11-16 23:51:05 -050086
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +053087#define MAYBE_BUILD_BUG_ON(cond) \
88 do { \
89 if (__builtin_constant_p((cond))) \
90 BUILD_BUG_ON(cond); \
91 else \
92 BUG_ON(cond); \
93 } while (0)
94
Paul Gortmaker35edd912011-11-16 23:51:05 -050095#endif /* __CHECKER__ */
96
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080097#ifdef CONFIG_GENERIC_BUG
98#include <asm-generic/bug.h>
99
100static inline int is_warning_bug(const struct bug_entry *bug)
101{
102 return bug->flags & BUGFLAG_WARNING;
103}
104
105const struct bug_entry *find_bug(unsigned long bugaddr);
106
Heiko Carstens608e2612007-07-15 23:41:39 -0700107enum bug_trap_type report_bug(unsigned long bug_addr, struct pt_regs *regs);
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -0800108
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -0800109/* These are defined by the architecture */
110int is_valid_bugaddr(unsigned long addr);
111
112#else /* !CONFIG_GENERIC_BUG */
113
Heiko Carstens608e2612007-07-15 23:41:39 -0700114static inline enum bug_trap_type report_bug(unsigned long bug_addr,
115 struct pt_regs *regs)
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -0800116{
117 return BUG_TRAP_TYPE_BUG;
118}
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -0800119
120#endif /* CONFIG_GENERIC_BUG */
121#endif /* _LINUX_BUG_H */