Added automatic userspace test launching capability to AKAROS.
[akaros.git] / kern / include / ros / common.h
1 #ifndef ROS_COMMON_H
2 #define ROS_COMMON_H
3
4 #ifndef __ASSEMBLER__
5 #ifndef __IVY__
6 #include <ros/noivy.h>
7 #endif
8
9 #include <stddef.h>
10 #include <stdint.h>
11 #include <sys/types.h>
12 #include <stdbool.h>
13
14 typedef uintptr_t physaddr_t;
15 typedef long intreg_t;
16 typedef unsigned long uintreg_t;
17
18 #ifndef NULL
19 #define NULL ((void*) 0)
20 #endif
21
22 #ifndef TRUE
23 #define TRUE    1
24 #endif
25
26 #ifndef FALSE
27 #define FALSE   0
28 #endif
29
30 #define KiB     1024u
31 #define MiB     1048576u
32 #define GiB     1073741824u
33 #define TiB     1099511627776ull
34 #define PiB     1125899906842624ull
35 #define EiB     1152921504606846976ull
36
37 #define ALIGNED(p, a)   (!(((uintptr_t)(p)) & ((a)-1)))
38
39 #define ARRAY_SIZE(x) (sizeof((x))/sizeof((x)[0]))
40
41 #define CHECK_FLAG(flags,bit)   ((flags) & (1 << (bit)))
42
43 #define FOR_CIRC_BUFFER(next, size, var) \
44         for (int _var = 0, var = (next); _var < (size); _var++, var = (var + 1) % (size))
45
46 #define STRINGIFY(s) __STRINGIFY(s)
47 #define __STRINGIFY(s) #s
48
49 // Efficient min and max operations
50 #ifdef ROS_KERNEL /* Glibc has their own */
51 #define MIN(_a, _b)                                             \
52 ({                                                              \
53         typeof(_a) __a = (_a);                                  \
54         typeof(_b) __b = (_b);                                  \
55         __a <= __b ? __a : __b;                                 \
56 })
57 #define MAX(_a, _b)                                             \
58 ({                                                              \
59         typeof(_a) __a = (_a);                                  \
60         typeof(_b) __b = (_b);                                  \
61         __a >= __b ? __a : __b;                                 \
62 })
63 #endif
64
65 /* Rounding operations (efficient when n is a power of 2)
66  * Round down to the nearest multiple of n.
67  * The compiler should compile out the branch.  This is needed for 32 bit, so
68  * that we can round down uint64_t, without chopping off the top 32 bits. */
69 #define ROUNDDOWN(a, n)                                                        \
70 ({                                                                             \
71         typeof(a) __b;                                                             \
72         if (sizeof(a) == 8) {                                                      \
73                 uint64_t __a = (uint64_t) (a);                                         \
74                 __b = (typeof(a)) (__a - __a % (n));                                   \
75         } else {                                                                   \
76                 uintptr_t __a = (uintptr_t) (a);                                       \
77                 __b = (typeof(a)) (__a - __a % (n));                                   \
78         }                                                                          \
79         __b;                                                                       \
80 })
81
82 /* Round up to the nearest multiple of n */
83 #define ROUNDUP(a, n)                                                          \
84 ({                                                                             \
85         typeof(a) __b;                                                             \
86         if (sizeof(a) == 8) {                                                      \
87                 uint64_t __n = (uint64_t) (n);                                         \
88                 __b = (typeof(a)) (ROUNDDOWN((uint64_t) (a) + __n - 1, __n));          \
89         } else {                                                                   \
90                 uintptr_t __n = (uintptr_t) (n);                                       \
91                 __b = (typeof(a)) (ROUNDDOWN((uintptr_t) (a) + __n - 1, __n));         \
92         }                                                                          \
93         __b;                                                                       \
94 })
95
96 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
97 // Return the integer logarithm of the value provided rounded down
98 static inline uintptr_t LOG2_DOWN(uintptr_t value)
99 {
100         uintptr_t l = 0;
101         while( (value >> l) > 1 ) ++l;
102         return l;
103 }
104
105 // Return the integer logarithm of the value provided rounded up
106 static inline uintptr_t LOG2_UP(uintptr_t value)
107 {
108         uintptr_t _v = LOG2_DOWN(value);
109         if (value ^ (1 << _v))
110                 return _v + 1;
111         else
112                 return _v;
113 }
114
115 static inline uintptr_t ROUNDUPPWR2(uintptr_t value)
116 {
117         return 1 << LOG2_UP(value);
118 }
119
120 static inline uintptr_t ROUNDDOWNPWR2(uintptr_t value)
121 {
122         return 1 << LOG2_DOWN(value);
123 }
124
125 /* We wraparound if UINT_MAX < a * b, which is also UINT_MAX / a < b. */
126 static inline bool mult_will_overflow_u64(uint64_t a, uint64_t b)
127 {
128         if (!a)
129                 return FALSE;
130         return (uint64_t)(-1) / a < b;
131 }
132
133 // Return the offset of 'member' relative to the beginning of a struct type
134 #ifndef offsetof
135 #define offsetof(type, member)  ((size_t) (&((type*)0)->member))
136 #endif
137
138 /* Return the container/struct holding the object 'ptr' points to */
139 #define container_of(ptr, type, member) ({                                     \
140         (type*)((char*)ptr - offsetof(type, member));                             \
141 })
142
143 /* Force the reading exactly once of x.  You may still need mbs().  See
144  * http://lwn.net/Articles/508991/ for more info. */
145 #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
146
147 /* Makes sure func is run exactly once.  Can handle concurrent callers, and
148  * other callers spin til the func is complete. */
149 #define run_once(func)                                                         \
150 {                                                                              \
151         static bool ran_once = FALSE;                                              \
152         static atomic_t is_running = FALSE;                                        \
153         if (!ran_once) {                                                           \
154                 if (!atomic_swap(&is_running, TRUE)) {                                 \
155                         /* we won the race and get to run the func */                      \
156                         func;                                                              \
157                         wmb();  /* don't let the ran_once write pass previous writes */    \
158                         ran_once = TRUE;                                                   \
159                 } else {                                                               \
160                         /* someone else won, wait til they are done to break out */        \
161                         while (!ran_once)                                                  \
162                                 cpu_relax();                                                   \
163                                                                                \
164                 }                                                                      \
165         }                                                                          \
166 }
167
168 /* Unprotected, single-threaded version, makes sure func is run exactly once */
169 #define run_once_racy(func)                                                    \
170 {                                                                              \
171         static bool ran_once = FALSE;                                              \
172         if (!ran_once) {                                                           \
173                 func;                                                                  \
174                 ran_once = TRUE;                                                       \
175         }                                                                          \
176 }
177
178 /* Aborts with 'retcmd' if this function has already been called.  Compared to
179  * run_once, this is put at the top of a function that can be called from
180  * multiple sources but should only execute once. */
181 #define init_once_racy(retcmd)                                                 \
182 {                                                                              \
183         static bool initialized = FALSE;                                           \
184         if (initialized) {                                                         \
185                 retcmd;                                                                \
186         }                                                                          \
187         initialized = TRUE;                                                        \
188 }
189
190 #endif /* __ASSEMBLER__ */
191
192 #endif /* ROS_COMMON_H */