| 1 |
/* Definitions borrowed from the Linux kernel, thus subject to GPLv2. */ |
| 2 |
|
| 3 |
#ifndef _STAPDYN_LINUX_DEFS_H_ |
| 4 |
#define _STAPDYN_LINUX_DEFS_H_ |
| 5 |
|
| 6 |
#include "linux_hash.h" |
| 7 |
|
| 8 |
#define min(x, y) ({ \ |
| 9 |
typeof(x) _min1 = (x); \ |
| 10 |
typeof(y) _min2 = (y); \ |
| 11 |
(void) (&_min1 == &_min2); \ |
| 12 |
_min1 < _min2 ? _min1 : _min2; }) |
| 13 |
|
| 14 |
#define max(x, y) ({ \ |
| 15 |
typeof(x) _max1 = (x); \ |
| 16 |
typeof(y) _max2 = (y); \ |
| 17 |
(void) (&_max1 == &_max2); \ |
| 18 |
_max1 > _max2 ? _max1 : _max2; }) |
| 19 |
|
| 20 |
#define min_t(type, x, y) ({ \ |
| 21 |
type __min1 = (x); \ |
| 22 |
type __min2 = (y); \ |
| 23 |
__min1 < __min2 ? __min1: __min2; }) |
| 24 |
|
| 25 |
#define max_t(type, x, y) ({ \ |
| 26 |
type __max1 = (x); \ |
| 27 |
type __max2 = (y); \ |
| 28 |
__max1 > __max2 ? __max1: __max2; }) |
| 29 |
|
| 30 |
#define clamp(val, min, max) ({ \ |
| 31 |
typeof(val) __val = (val); \ |
| 32 |
typeof(min) __min = (min); \ |
| 33 |
typeof(max) __max = (max); \ |
| 34 |
(void) (&__val == &__min); \ |
| 35 |
(void) (&__val == &__max); \ |
| 36 |
__val = __val < __min ? __min: __val; \ |
| 37 |
__val > __max ? __max: __val; }) |
| 38 |
|
| 39 |
#define clamp_t(type, val, min, max) ({ \ |
| 40 |
type __val = (val); \ |
| 41 |
type __min = (min); \ |
| 42 |
type __max = (max); \ |
| 43 |
__val = __val < __min ? __min: __val; \ |
| 44 |
__val > __max ? __max: __val; }) |
| 45 |
|
| 46 |
#define likely(x) __builtin_expect(!!(x), 1) |
| 47 |
#define unlikely(x) __builtin_expect(!!(x), 0) |
| 48 |
|
| 49 |
#define container_of(ptr, type, member) ({ \ |
| 50 |
const typeof( ((type *)0)->member ) *__mptr = (ptr); \ |
| 51 |
(type *)( (char *)__mptr - offsetof(type,member) );}) |
| 52 |
|
| 53 |
#ifndef CLOCK_MONOTONIC_RAW |
| 54 |
#define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC |
| 55 |
#endif |
| 56 |
|
| 57 |
#define noinline __attribute__((noinline)) |
| 58 |
|
| 59 |
#define ATOMIC_INIT(i) { (i) } |
| 60 |
|
| 61 |
static inline void atomic_inc(atomic_t *v) |
| 62 |
{ |
| 63 |
atomic_add_return(1, v); |
| 64 |
} |
| 65 |
|
| 66 |
static inline void atomic_dec(atomic_t *v) |
| 67 |
{ |
| 68 |
atomic_sub_return(1, v); |
| 69 |
} |
| 70 |
|
| 71 |
static inline int atomic_read(const atomic_t *v) |
| 72 |
{ |
| 73 |
return (*(volatile int *)&(v)->counter); |
| 74 |
} |
| 75 |
|
| 76 |
static inline void atomic_set(atomic_t *v, int i) |
| 77 |
{ |
| 78 |
v->counter = i; |
| 79 |
} |
| 80 |
|
| 81 |
static inline int atomic_cmpxchg(atomic_t *v, int old, int new) |
| 82 |
{ |
| 83 |
int prev = atomic_read(v); |
| 84 |
if (prev == old) |
| 85 |
atomic_set(v, new); |
| 86 |
return prev; |
| 87 |
} |
| 88 |
|
| 89 |
#define atomic_inc_return(v) atomic_add_return(1, (v)) |
| 90 |
|
| 91 |
|
| 92 |
#define do_div(n,base) ({ \ |
| 93 |
uint32_t __base = (base); \ |
| 94 |
uint32_t __rem; \ |
| 95 |
__rem = ((uint64_t)(n)) % __base; \ |
| 96 |
(n) = ((uint64_t)(n)) / __base; \ |
| 97 |
__rem; \ |
| 98 |
}) |
| 99 |
|
| 100 |
#if !__GLIBC_PREREQ(2, 38) |
| 101 |
static inline size_t strlcpy(char *dest, const char *src, size_t size) |
| 102 |
{ |
| 103 |
size_t ret = strlen(src); |
| 104 |
|
| 105 |
if (size) { |
| 106 |
size_t len = (ret >= size) ? size - 1 : ret; |
| 107 |
memcpy(dest, src, len); |
| 108 |
dest[len] = '\0'; |
| 109 |
} |
| 110 |
return ret; |
| 111 |
} |
| 112 |
|
| 113 |
static inline size_t strlcat(char *dest, const char *src, size_t count) |
| 114 |
{ |
| 115 |
size_t dsize = strlen(dest); |
| 116 |
size_t len = strlen(src); |
| 117 |
size_t res = dsize + len; |
| 118 |
|
| 119 |
/* This would be a bug */ |
| 120 |
//BUG_ON(dsize >= count); |
| 121 |
dsize = min(dsize, count); |
| 122 |
|
| 123 |
dest += dsize; |
| 124 |
count -= dsize; |
| 125 |
if (len >= count) |
| 126 |
len = count-1; |
| 127 |
memcpy(dest, src, len); |
| 128 |
dest[len] = 0; |
| 129 |
return res; |
| 130 |
} |
| 131 |
#endif |
| 132 |
|
| 133 |
#define __must_check __attribute__((warn_unused_result)) |
| 134 |
# define __force |
| 135 |
# define __user |
| 136 |
# define __chk_user_ptr(x) (void)0 |
| 137 |
#define user_mode(regs) 1 |
| 138 |
#define __get_user(x, ptr) \ |
| 139 |
({ \ |
| 140 |
int __gu_err = -EFAULT; \ |
| 141 |
__chk_user_ptr(ptr); \ |
| 142 |
switch (sizeof(*(ptr))) { \ |
| 143 |
case 1: { \ |
| 144 |
unsigned char __x; \ |
| 145 |
__gu_err = __get_user_fn(sizeof (*(ptr)), \ |
| 146 |
ptr, &__x); \ |
| 147 |
(x) = *(__force __typeof__(*(ptr)) *) &__x; \ |
| 148 |
break; \ |
| 149 |
}; \ |
| 150 |
case 2: { \ |
| 151 |
unsigned short __x; \ |
| 152 |
__gu_err = __get_user_fn(sizeof (*(ptr)), \ |
| 153 |
ptr, &__x); \ |
| 154 |
(x) = *(__force __typeof__(*(ptr)) *) &__x; \ |
| 155 |
break; \ |
| 156 |
}; \ |
| 157 |
case 4: { \ |
| 158 |
unsigned int __x; \ |
| 159 |
__gu_err = __get_user_fn(sizeof (*(ptr)), \ |
| 160 |
ptr, &__x); \ |
| 161 |
(x) = *(__force __typeof__(*(ptr)) *) &__x; \ |
| 162 |
break; \ |
| 163 |
}; \ |
| 164 |
case 8: { \ |
| 165 |
unsigned long long __x; \ |
| 166 |
__gu_err = __get_user_fn(sizeof (*(ptr)), \ |
| 167 |
ptr, &__x); \ |
| 168 |
(x) = *(__force __typeof__(*(ptr)) *) &__x; \ |
| 169 |
break; \ |
| 170 |
}; \ |
| 171 |
default: \ |
| 172 |
__get_user_bad(); \ |
| 173 |
break; \ |
| 174 |
} \ |
| 175 |
__gu_err; \ |
| 176 |
}) |
| 177 |
|
| 178 |
static inline __must_check long __copy_from_user(void *to, |
| 179 |
const void __user * from, unsigned long n); |
| 180 |
|
| 181 |
static inline int __get_user_fn(size_t size, const void __user *ptr, void *x) |
| 182 |
{ |
| 183 |
size = __copy_from_user(x, ptr, size); |
| 184 |
return size ? -EFAULT : size; |
| 185 |
} |
| 186 |
|
| 187 |
extern int __get_user_bad(void) __attribute__((noreturn)); |
| 188 |
|
| 189 |
#define __put_user(x, ptr) \ |
| 190 |
({ \ |
| 191 |
int __gu_err = -EFAULT; \ |
| 192 |
__chk_user_ptr(ptr); \ |
| 193 |
switch (sizeof(*(ptr))) { \ |
| 194 |
case 1: { \ |
| 195 |
unsigned char __x = (unsigned char)(x); \ |
| 196 |
__gu_err = __put_user_fn(sizeof (*(ptr)), \ |
| 197 |
ptr, &__x); \ |
| 198 |
break; \ |
| 199 |
}; \ |
| 200 |
case 2: { \ |
| 201 |
unsigned short __x = (unsigned short)(x); \ |
| 202 |
__gu_err = __put_user_fn(sizeof (*(ptr)), \ |
| 203 |
ptr, &__x); \ |
| 204 |
break; \ |
| 205 |
}; \ |
| 206 |
case 4: { \ |
| 207 |
unsigned int __x = (unsigned int)(x); \ |
| 208 |
__gu_err = __put_user_fn(sizeof (*(ptr)), \ |
| 209 |
ptr, &__x); \ |
| 210 |
break; \ |
| 211 |
}; \ |
| 212 |
case 8: { \ |
| 213 |
unsigned long long __x = (unsigned long long)(x); \ |
| 214 |
__gu_err = __put_user_fn(sizeof (*(ptr)), \ |
| 215 |
ptr, &__x); \ |
| 216 |
break; \ |
| 217 |
}; \ |
| 218 |
default: \ |
| 219 |
__put_user_bad(); \ |
| 220 |
break; \ |
| 221 |
} \ |
| 222 |
__gu_err; \ |
| 223 |
}) |
| 224 |
|
| 225 |
static inline __must_check long __copy_to_user(void *to, const void *from, |
| 226 |
unsigned long n); |
| 227 |
|
| 228 |
static inline int __put_user_fn(size_t size, const void __user *ptr, void *x) |
| 229 |
{ |
| 230 |
size = __copy_to_user(x, ptr, size); |
| 231 |
return size ? -EFAULT : size; |
| 232 |
} |
| 233 |
|
| 234 |
extern int __put_user_bad(void) __attribute__((noreturn)); |
| 235 |
|
| 236 |
|
| 237 |
#endif /* _STAPDYN_LINUX_DEFS_H_ */ |
| 238 |
|