diff options
Diffstat (limited to 'sys/compat/linuxkpi/common/include/linux/kernel.h')
-rw-r--r-- | sys/compat/linuxkpi/common/include/linux/kernel.h | 199 |
1 files changed, 64 insertions, 135 deletions
diff --git a/sys/compat/linuxkpi/common/include/linux/kernel.h b/sys/compat/linuxkpi/common/include/linux/kernel.h index 4987c582f0f3..ed4320e80fa7 100644 --- a/sys/compat/linuxkpi/common/include/linux/kernel.h +++ b/sys/compat/linuxkpi/common/include/linux/kernel.h @@ -26,13 +26,10 @@ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * $FreeBSD$ */ #ifndef _LINUXKPI_LINUX_KERNEL_H_ #define _LINUXKPI_LINUX_KERNEL_H_ -#include <sys/cdefs.h> #include <sys/types.h> #include <sys/systm.h> #include <sys/param.h> @@ -44,18 +41,25 @@ #include <sys/time.h> #include <linux/bitops.h> +#include <linux/build_bug.h> #include <linux/compiler.h> +#include <linux/container_of.h> +#include <linux/limits.h> #include <linux/stringify.h> #include <linux/errno.h> #include <linux/sched.h> #include <linux/types.h> +#include <linux/typecheck.h> #include <linux/jiffies.h> #include <linux/log2.h> +#include <linux/kconfig.h> #include <asm/byteorder.h> +#include <asm/cpufeature.h> +#include <asm/processor.h> #include <asm/uaccess.h> -#include <machine/stdarg.h> +#include <linux/stdarg.h> #define KERN_CONT "" #define KERN_EMERG "<0>" @@ -67,19 +71,6 @@ #define KERN_INFO "<6>" #define KERN_DEBUG "<7>" -#define U8_MAX ((u8)~0U) -#define S8_MAX ((s8)(U8_MAX >> 1)) -#define S8_MIN ((s8)(-S8_MAX - 1)) -#define U16_MAX ((u16)~0U) -#define S16_MAX ((s16)(U16_MAX >> 1)) -#define S16_MIN ((s16)(-S16_MAX - 1)) -#define U32_MAX ((u32)~0U) -#define S32_MAX ((s32)(U32_MAX >> 1)) -#define S32_MIN ((s32)(-S32_MAX - 1)) -#define U64_MAX ((u64)~0ULL) -#define S64_MAX ((s64)(U64_MAX >> 1)) -#define S64_MIN ((s64)(-S64_MAX - 1)) - #define S8_C(x) x #define U8_C(x) x ## U #define S16_C(x) x @@ -89,28 +80,6 @@ #define S64_C(x) x ## LL #define U64_C(x) x ## ULL -/* - * BUILD_BUG_ON() can happen inside functions where _Static_assert() does not - * seem to work. Use old-schoold-ish CTASSERT from before commit - * a3085588a88fa58eb5b1eaae471999e1995a29cf but also make sure we do not - * end up with an unused typedef or variable. The compiler should optimise - * it away entirely. - */ -#define _O_CTASSERT(x) _O__CTASSERT(x, __LINE__) -#define _O__CTASSERT(x, y) _O___CTASSERT(x, y) -#define _O___CTASSERT(x, y) while (0) { \ - typedef char __assert_line_ ## y[(x) ? 1 : -1]; \ - __assert_line_ ## y _x; \ - _x[0] = '\0'; \ -} - -#define BUILD_BUG() do { CTASSERT(0); } while (0) -#define BUILD_BUG_ON(x) do { _O_CTASSERT(!(x)) } while (0) -#define BUILD_BUG_ON_MSG(x, msg) BUILD_BUG_ON(x) -#define BUILD_BUG_ON_NOT_POWER_OF_2(x) BUILD_BUG_ON(!powerof2(x)) -#define BUILD_BUG_ON_INVALID(expr) while (0) { (void)(expr); } -#define BUILD_BUG_ON_ZERO(x) ((int)sizeof(struct { int:-((x) != 0); })) - #define BUG() panic("BUG at %s:%d", __FILE__, __LINE__) #define BUG_ON(cond) do { \ if (cond) { \ @@ -163,6 +132,8 @@ extern int linuxkpi_warn_dump_stack; #define printk(...) printf(__VA_ARGS__) #define vprintk(f, a) vprintf(f, a) +#define PTR_IF(x, p) ((x) ? (p) : NULL) + #define asm __asm extern void linux_dump_stack(void); @@ -293,12 +264,6 @@ extern int linuxkpi_debug; }) #endif -#define container_of(ptr, type, member) \ -({ \ - const __typeof(((type *)0)->member) *__p = (ptr); \ - (type *)((uintptr_t)__p - offsetof(type, member)); \ -}) - #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #define u64_to_user_ptr(val) ((void *)(uintptr_t)(val)) @@ -434,19 +399,8 @@ kstrtou16(const char *cp, unsigned int base, u16 *res) static inline int kstrtou32(const char *cp, unsigned int base, u32 *res) { - char *end; - unsigned long temp; - *res = temp = strtoul(cp, &end, base); - - /* skip newline character, if any */ - if (*end == '\n') - end++; - if (*cp == 0 || *end != 0) - return (-EINVAL); - if (temp != (u32)temp) - return (-ERANGE); - return (0); + return (kstrtouint(cp, base, res)); } static inline int @@ -527,7 +481,7 @@ kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, static inline int kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, - int *p) + unsigned int *p) { char buf[36] = {}; @@ -541,6 +495,14 @@ kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, } static inline int +kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, + unsigned int *p) +{ + + return (kstrtouint_from_user(s, count, base, p)); +} + +static inline int kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *p) { @@ -622,12 +584,6 @@ mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor) return ((q * multiplier) + ((r * multiplier) / divisor)); } -static inline int64_t -abs64(int64_t x) -{ - return (x < 0 ? -x : x); -} - typedef struct linux_ratelimit { struct timeval lasttime; int counter; @@ -639,12 +595,6 @@ linux_ratelimited(linux_ratelimit_t *rl) return (ppsratecheck(&rl->lasttime, &rl->counter, 1)); } -#define struct_size(ptr, field, num) ({ \ - const size_t __size = offsetof(__typeof(*(ptr)), field); \ - const size_t __max = (SIZE_MAX - __size) / sizeof((ptr)->field[0]); \ - ((num) > __max) ? SIZE_MAX : (__size + sizeof((ptr)->field[0]) * (num)); \ -}) - #define __is_constexpr(x) \ __builtin_constant_p(x) @@ -654,31 +604,10 @@ linux_ratelimited(linux_ratelimit_t *rl) */ #define is_signed(datatype) (((datatype)-1 / (datatype)2) == (datatype)0) -/* - * The type_max() macro below returns the maxium positive value the - * passed data type can hold. - */ -#define type_max(datatype) ( \ - (sizeof(datatype) >= 8) ? (is_signed(datatype) ? INT64_MAX : UINT64_MAX) : \ - (sizeof(datatype) >= 4) ? (is_signed(datatype) ? INT32_MAX : UINT32_MAX) : \ - (sizeof(datatype) >= 2) ? (is_signed(datatype) ? INT16_MAX : UINT16_MAX) : \ - (is_signed(datatype) ? INT8_MAX : UINT8_MAX) \ -) - -/* - * The type_min() macro below returns the minimum value the passed - * data type can hold. For unsigned types the minimum value is always - * zero. For signed types it may vary. - */ -#define type_min(datatype) ( \ - (sizeof(datatype) >= 8) ? (is_signed(datatype) ? INT64_MIN : 0) : \ - (sizeof(datatype) >= 4) ? (is_signed(datatype) ? INT32_MIN : 0) : \ - (sizeof(datatype) >= 2) ? (is_signed(datatype) ? INT16_MIN : 0) : \ - (is_signed(datatype) ? INT8_MIN : 0) \ -) - #define TAINT_WARN 0 #define test_taint(x) (0) +#define add_taint(x,y) do { \ + } while (0) static inline int _h2b(const char c) @@ -711,49 +640,49 @@ hex2bin(uint8_t *bindst, const char *hexsrc, size_t binlen) return (0); } +static inline bool +mac_pton(const char *macin, uint8_t *macout) +{ + const char *s, *d; + uint8_t mac[6], hx, lx;; + int i; + + if (strlen(macin) < (3 * 6 - 1)) + return (false); + + i = 0; + s = macin; + do { + /* Should we also support '-'-delimiters? */ + d = strchrnul(s, ':'); + hx = lx = 0; + while (s < d) { + /* Fail on abc:123:xxx:... */ + if ((d - s) > 2) + return (false); + /* We do support non-well-formed strings: 3:45:6:... */ + if ((d - s) > 1) { + hx = _h2b(*s); + if (hx < 0) + return (false); + s++; + } + lx = _h2b(*s); + if (lx < 0) + return (false); + s++; + } + mac[i] = (hx << 4) | lx; + i++; + if (i >= 6) + return (false); + } while (d != NULL && *d != '\0'); + + memcpy(macout, mac, 6); + return (true); +} + #define DECLARE_FLEX_ARRAY(_t, _n) \ struct { struct { } __dummy_ ## _n; _t _n[0]; } -/* - * Checking if an option is defined would be easy if we could do CPP inside CPP. - * The defined case whether -Dxxx or -Dxxx=1 are easy to deal with. In either - * case the defined value is "1". A more general -Dxxx=<c> case will require - * more effort to deal with all possible "true" values. Hope we do not have - * to do this as well. - * The real problem is the undefined case. To avoid this problem we do the - * concat/varargs trick: "yyy" ## xxx can make two arguments if xxx is "1" - * by having a #define for yyy_1 which is "ignore,". - * Otherwise we will just get "yyy". - * Need to be careful about variable substitutions in macros though. - * This way we make a (true, false) problem a (don't care, true, false) or a - * (don't care true, false). Then we can use a variadic macro to only select - * the always well known and defined argument #2. And that seems to be - * exactly what we need. Use 1 for true and 0 for false to also allow - * #if IS_*() checks pre-compiler checks which do not like #if true. - */ -#define ___XAB_1 dontcare, -#define ___IS_XAB(_ignore, _x, ...) (_x) -#define __IS_XAB(_x) ___IS_XAB(_x 1, 0) -#define _IS_XAB(_x) __IS_XAB(__CONCAT(___XAB_, _x)) - -/* This is if CONFIG_ccc=y. */ -#define IS_BUILTIN(_x) _IS_XAB(_x) -/* This is if CONFIG_ccc=m. */ -#define IS_MODULE(_x) _IS_XAB(_x ## _MODULE) -/* This is if CONFIG_ccc is compiled in(=y) or a module(=m). */ -#define IS_ENABLED(_x) (IS_BUILTIN(_x) || IS_MODULE(_x)) -/* - * This is weird case. If the CONFIG_ccc is builtin (=y) this returns true; - * or if the CONFIG_ccc is a module (=m) and the caller is built as a module - * (-DMODULE defined) this returns true, but if the callers is not a module - * (-DMODULE not defined, which means caller is BUILTIN) then it returns - * false. In other words, a module can reach the kernel, a module can reach - * a module, but the kernel cannot reach a module, and code never compiled - * cannot be reached either. - * XXX -- I'd hope the module-to-module case would be handled by a proper - * module dependency definition (MODULE_DEPEND() in FreeBSD). - */ -#define IS_REACHABLE(_x) (IS_BUILTIN(_x) || \ - (IS_MODULE(_x) && IS_BUILTIN(MODULE))) - #endif /* _LINUXKPI_LINUX_KERNEL_H_ */ |