| 1 | #ifndef KERNELCOMPAT_H |
| 2 | #define KERNELCOMPAT_H |
| 3 | |
| 4 | #include <compiler.h> |
| 5 | #include <kcompat.h> |
| 6 | |
| 7 | #include <string.h> |
| 8 | #include <sys/time.h> |
| 9 | |
| 10 | /* FIXME: libkcompat must not define arch-specific local ops, as ust *must* |
| 11 | * fallback to the normal atomic ops. Fix things so we don't add them and |
| 12 | * break things accidentally. |
| 13 | */ |
| 14 | |
| 15 | #define container_of(ptr, type, member) ({ \ |
| 16 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ |
| 17 | (type *)( (char *)__mptr - offsetof(type,member) );}) |
| 18 | |
| 19 | #define KERN_DEBUG "" |
| 20 | #define KERN_NOTICE "" |
| 21 | #define KERN_INFO "" |
| 22 | #define KERN_ERR "" |
| 23 | #define KERN_ALERT "" |
| 24 | #define KERN_WARNING "" |
| 25 | |
| 26 | /* ERROR OPS */ |
| 27 | |
| 28 | #define MAX_ERRNO 4095 |
| 29 | |
| 30 | #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO) |
| 31 | |
| 32 | static inline void *ERR_PTR(long error) |
| 33 | { |
| 34 | return (void *) error; |
| 35 | } |
| 36 | |
| 37 | static inline long PTR_ERR(const void *ptr) |
| 38 | { |
| 39 | return (long) ptr; |
| 40 | } |
| 41 | |
| 42 | static inline long IS_ERR(const void *ptr) |
| 43 | { |
| 44 | return IS_ERR_VALUE((unsigned long)ptr); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | /* Min / Max */ |
| 49 | |
| 50 | #define min_t(type, x, y) ({ \ |
| 51 | type __min1 = (x); \ |
| 52 | type __min2 = (y); \ |
| 53 | __min1 < __min2 ? __min1: __min2; }) |
| 54 | |
| 55 | #define max_t(type, x, y) ({ \ |
| 56 | type __max1 = (x); \ |
| 57 | type __max2 = (y); \ |
| 58 | __max1 > __max2 ? __max1: __max2; }) |
| 59 | |
| 60 | |
| 61 | /* MUTEXES */ |
| 62 | |
| 63 | #include <pthread.h> |
| 64 | |
| 65 | #define DEFINE_MUTEX(m) pthread_mutex_t (m) = PTHREAD_MUTEX_INITIALIZER; |
| 66 | #define DECLARE_MUTEX(m) extern pthread_mutex_t (m); |
| 67 | |
| 68 | #define mutex_lock(m) pthread_mutex_lock(m) |
| 69 | |
| 70 | #define mutex_unlock(m) pthread_mutex_unlock(m) |
| 71 | |
| 72 | |
| 73 | /* MALLOCATION */ |
| 74 | |
| 75 | #include <stdlib.h> |
| 76 | |
| 77 | #define kmalloc(s, t) malloc(s) |
| 78 | #define kzalloc(s, t) zmalloc(s) |
| 79 | #define kfree(p) free((void *)p) |
| 80 | #define kstrdup(s, t) strdup(s) |
| 81 | |
| 82 | #define zmalloc(s) calloc(1, s) |
| 83 | |
| 84 | #define GFP_KERNEL |
| 85 | |
| 86 | /* PRINTK */ |
| 87 | |
| 88 | #include <stdio.h> |
| 89 | #define printk(fmt, args...) printf(fmt, ## args) |
| 90 | |
| 91 | |
| 92 | /* ATTRIBUTES */ |
| 93 | |
| 94 | #define ____cacheline_aligned |
| 95 | |
| 96 | /* MATH */ |
| 97 | |
| 98 | static inline unsigned int hweight32(unsigned int w) |
| 99 | { |
| 100 | unsigned int res = w - ((w >> 1) & 0x55555555); |
| 101 | res = (res & 0x33333333) + ((res >> 2) & 0x33333333); |
| 102 | res = (res + (res >> 4)) & 0x0F0F0F0F; |
| 103 | res = res + (res >> 8); |
| 104 | return (res + (res >> 16)) & 0x000000FF; |
| 105 | } |
| 106 | |
| 107 | static inline int fls(int x) |
| 108 | { |
| 109 | int r; |
| 110 | //ust// #ifdef CONFIG_X86_CMOV |
| 111 | asm("bsrl %1,%0\n\t" |
| 112 | "cmovzl %2,%0" |
| 113 | : "=&r" (r) : "rm" (x), "rm" (-1)); |
| 114 | //ust// #else |
| 115 | //ust// asm("bsrl %1,%0\n\t" |
| 116 | //ust// "jnz 1f\n\t" |
| 117 | //ust// "movl $-1,%0\n" |
| 118 | //ust// "1:" : "=r" (r) : "rm" (x)); |
| 119 | //ust// #endif |
| 120 | return r + 1; |
| 121 | } |
| 122 | |
| 123 | static __inline__ int get_count_order(unsigned int count) |
| 124 | { |
| 125 | int order; |
| 126 | |
| 127 | order = fls(count) - 1; |
| 128 | if (count & (count - 1)) |
| 129 | order++; |
| 130 | return order; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | #include <unistd.h> |
| 137 | |
| 138 | #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1) |
| 139 | #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) |
| 140 | #define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE) |
| 141 | #define PAGE_SIZE sysconf(_SC_PAGE_SIZE) |
| 142 | #define PAGE_MASK (~(PAGE_SIZE-1)) |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | /* ARRAYS */ |
| 148 | |
| 149 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) |
| 150 | |
| 151 | /* TRACE CLOCK */ |
| 152 | |
| 153 | /* There are two types of clocks that can be used. |
| 154 | - TSC based clock |
| 155 | - gettimeofday() clock |
| 156 | |
| 157 | Microbenchmarks on Linux 2.6.30 on Core2 Duo 3GHz (functions are inlined): |
| 158 | Calls (100000000) to tsc(): 4004035641 cycles or 40 cycles/call |
| 159 | Calls (100000000) to gettimeofday(): 9723158352 cycles or 97 cycles/call |
| 160 | |
| 161 | For merging traces with the kernel, a time source compatible with that of |
| 162 | the kernel is necessary. |
| 163 | |
| 164 | */ |
| 165 | |
| 166 | #if 0 |
| 167 | /* WARNING: Make sure to set frequency and scaling functions that will not |
| 168 | * result in lttv timestamps (sec.nsec) with seconds greater than 2**32-1. |
| 169 | */ |
| 170 | static inline u64 trace_clock_read64(void) |
| 171 | { |
| 172 | uint32_t low; |
| 173 | uint32_t high; |
| 174 | uint64_t retval; |
| 175 | __asm__ volatile ("rdtsc\n" : "=a" (low), "=d" (high)); |
| 176 | |
| 177 | retval = high; |
| 178 | retval <<= 32; |
| 179 | return retval | low; |
| 180 | } |
| 181 | #endif |
| 182 | |
| 183 | static inline u64 trace_clock_read64(void) |
| 184 | { |
| 185 | struct timeval tv; |
| 186 | u64 retval; |
| 187 | |
| 188 | gettimeofday(&tv, NULL); |
| 189 | retval = tv.tv_sec; |
| 190 | retval *= 1000000; |
| 191 | retval += tv.tv_usec; |
| 192 | |
| 193 | return retval; |
| 194 | } |
| 195 | |
| 196 | static inline u64 trace_clock_frequency(void) |
| 197 | { |
| 198 | return 1000000LL; |
| 199 | } |
| 200 | |
| 201 | static inline u32 trace_clock_freq_scale(void) |
| 202 | { |
| 203 | return 1; |
| 204 | } |
| 205 | |
| 206 | |
| 207 | #endif /* KERNELCOMPAT_H */ |