kern: mmap: make the lowest mmap address MiB; mmap ld.so at MiB
[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 /* A macro for testing if another macro has been #defined or not.  Can be used
50  * wherever you need a boolean defined.  Returns 0 or 1. */
51 #define is_defined(macro) is_defined_(macro)
52 #define is_defined_test_1 ,
53 #define is_defined_(value) is_defined__(is_defined_test_##value, value)
54 #define is_defined__(comma, value) is_defined___(comma 1, 0)
55 #define is_defined___(_, v, ...) v
56
57 // Efficient min and max operations
58 #ifdef ROS_KERNEL /* Glibc has their own */
59 #define MIN(_a, _b)                                             \
60 ({                                                              \
61         typeof(_a) __a = (_a);                                  \
62         typeof(_b) __b = (_b);                                  \
63         __a <= __b ? __a : __b;                                 \
64 })
65 #define MAX(_a, _b)                                             \
66 ({                                                              \
67         typeof(_a) __a = (_a);                                  \
68         typeof(_b) __b = (_b);                                  \
69         __a >= __b ? __a : __b;                                 \
70 })
71 #endif
72
73 /* Rounding operations (efficient when n is a power of 2)
74  * Round down to the nearest multiple of n.
75  * The compiler should compile out the branch.  This is needed for 32 bit, so
76  * that we can round down uint64_t, without chopping off the top 32 bits. */
77 #define ROUNDDOWN(a, n)                                                        \
78 ({                                                                             \
79         typeof(a) __b;                                                             \
80         if (sizeof(a) == 8) {                                                      \
81                 uint64_t __a = (uint64_t) (a);                                         \
82                 __b = (typeof(a)) (__a - __a % (n));                                   \
83         } else {                                                                   \
84                 uintptr_t __a = (uintptr_t) (a);                                       \
85                 __b = (typeof(a)) (__a - __a % (n));                                   \
86         }                                                                          \
87         __b;                                                                       \
88 })
89
90 /* Round up to the nearest multiple of n */
91 #define ROUNDUP(a, n)                                                          \
92 ({                                                                             \
93         typeof(a) __b;                                                             \
94         if (sizeof(a) == 8) {                                                      \
95                 uint64_t __n = (uint64_t) (n);                                         \
96                 __b = (typeof(a)) (ROUNDDOWN((uint64_t) (a) + __n - 1, __n));          \
97         } else {                                                                   \
98                 uintptr_t __n = (uintptr_t) (n);                                       \
99                 __b = (typeof(a)) (ROUNDDOWN((uintptr_t) (a) + __n - 1, __n));         \
100         }                                                                          \
101         __b;                                                                       \
102 })
103
104 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
105 // Return the integer logarithm of the value provided rounded down
106 static inline uintptr_t LOG2_DOWN(uintptr_t value)
107 {
108         uintptr_t l = 0;
109         while( (value >> l) > 1 ) ++l;
110         return l;
111 }
112
113 // Return the integer logarithm of the value provided rounded up
114 static inline uintptr_t LOG2_UP(uintptr_t value)
115 {
116         uintptr_t _v = LOG2_DOWN(value);
117         if (value ^ (1 << _v))
118                 return _v + 1;
119         else
120                 return _v;
121 }
122
123 static inline uintptr_t ROUNDUPPWR2(uintptr_t value)
124 {
125         return 1 << LOG2_UP(value);
126 }
127
128 static inline uintptr_t ROUNDDOWNPWR2(uintptr_t value)
129 {
130         return 1 << LOG2_DOWN(value);
131 }
132
133 /* We wraparound if UINT_MAX < a * b, which is also UINT_MAX / a < b. */
134 static inline bool mult_will_overflow_u64(uint64_t a, uint64_t b)
135 {
136         if (!a)
137                 return FALSE;
138         return (uint64_t)(-1) / a < b;
139 }
140
141 // Return the offset of 'member' relative to the beginning of a struct type
142 #ifndef offsetof
143 #define offsetof(type, member)  ((size_t) (&((type*)0)->member))
144 #endif
145
146 /* Return the container/struct holding the object 'ptr' points to */
147 #define container_of(ptr, type, member) ({                                     \
148         (type*)((char*)ptr - offsetof(type, member));                             \
149 })
150
151 /* Force the reading exactly once of x.  You may still need mbs().  See
152  * http://lwn.net/Articles/508991/ for more info. */
153 #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
154
155 /* Makes sure func is run exactly once.  Can handle concurrent callers, and
156  * other callers spin til the func is complete. */
157 #define run_once(func)                                                         \
158 {                                                                              \
159         static bool ran_once = FALSE;                                              \
160         static atomic_t is_running = FALSE;                                        \
161         if (!ran_once) {                                                           \
162                 if (!atomic_swap(&is_running, TRUE)) {                                 \
163                         /* we won the race and get to run the func */                      \
164                         func;                                                              \
165                         wmb();  /* don't let the ran_once write pass previous writes */    \
166                         ran_once = TRUE;                                                   \
167                 } else {                                                               \
168                         /* someone else won, wait til they are done to break out */        \
169                         while (!ran_once)                                                  \
170                                 cpu_relax();                                                   \
171                                                                                \
172                 }                                                                      \
173         }                                                                          \
174 }
175
176 /* Unprotected, single-threaded version, makes sure func is run exactly once */
177 #define run_once_racy(func)                                                    \
178 {                                                                              \
179         static bool ran_once = FALSE;                                              \
180         if (!ran_once) {                                                           \
181                 func;                                                                  \
182                 ran_once = TRUE;                                                       \
183         }                                                                          \
184 }
185
186 /* Aborts with 'retcmd' if this function has already been called.  Compared to
187  * run_once, this is put at the top of a function that can be called from
188  * multiple sources but should only execute once. */
189 #define init_once_racy(retcmd)                                                 \
190 {                                                                              \
191         static bool initialized = FALSE;                                           \
192         if (initialized) {                                                         \
193                 retcmd;                                                                \
194         }                                                                          \
195         initialized = TRUE;                                                        \
196 }
197
198 #endif /* __ASSEMBLER__ */
199
200 #endif /* ROS_COMMON_H */