Sanitize vcoreid from untrusted sources
[akaros.git] / kern / include / ros / common.h
1 #pragma once
2
3 #ifndef __ASSEMBLER__
4
5 #include <stddef.h>
6 #include <stdint.h>
7 #include <sys/types.h>
8 #include <stdbool.h>
9 #include <string.h>
10
11 typedef uintptr_t physaddr_t;
12 typedef long intreg_t;
13 typedef unsigned long uintreg_t;
14
15 #ifndef NULL
16 #define NULL ((void*) 0)
17 #endif
18
19 #ifndef TRUE
20 #define TRUE    1
21 #endif
22
23 #ifndef FALSE
24 #define FALSE   0
25 #endif
26
27 #define FOR_CIRC_BUFFER(next, size, var) \
28         for (int _var = 0, var = (next); _var < (size); _var++, var = (var + 1) % (size))
29
30 #define STRINGIFY(s) __STRINGIFY(s)
31 #define __STRINGIFY(s) #s
32
33 /* A macro for testing if another macro has been #defined or not.  Can be used
34  * wherever you need a boolean defined.  Returns 0 or 1. */
35 #define is_defined(macro) is_defined_(macro)
36 #define is_defined_test_1 ,
37 #define is_defined_(value) is_defined__(is_defined_test_##value, value)
38 #define is_defined__(comma, value) is_defined___(comma 1, 0)
39 #define is_defined___(_, v, ...) v
40
41 #define COUNT_OF(x) (sizeof((x))/sizeof((x)[0]))
42
43 #define ZERO_DATA(s) memset(&(s), 0, sizeof(s))
44
45 /* Rounding operations (efficient when n is a power of 2)
46  * Round down to the nearest multiple of n.
47  * The compiler should compile out the branch.  This is needed for 32 bit, so
48  * that we can round down uint64_t, without chopping off the top 32 bits. */
49 #define ROUNDDOWN(a, n)                                                        \
50 ({                                                                             \
51         typeof(a) __b;                                                         \
52         if (sizeof(a) == 8) {                                                  \
53                 uint64_t __a = (uint64_t) (a);                                 \
54                 __b = (typeof(a)) (__a - __a % (n));                           \
55         } else {                                                               \
56                 uintptr_t __a = (uintptr_t) (a);                               \
57                 __b = (typeof(a)) (__a - __a % (n));                           \
58         }                                                                      \
59         __b;                                                                   \
60 })
61
62 /* Round up to the nearest multiple of n */
63 #define ROUNDUP(a, n)                                                          \
64 ({                                                                             \
65         typeof(a) __b;                                                         \
66         if (sizeof(a) == 8) {                                                  \
67                 uint64_t __n = (uint64_t) (n);                                 \
68                 __b = (typeof(a)) (ROUNDDOWN((uint64_t) (a) + __n - 1, __n));  \
69         } else {                                                               \
70                 uintptr_t __n = (uintptr_t) (n);                               \
71                 __b = (typeof(a)) (ROUNDDOWN((uintptr_t) (a) + __n - 1, __n)); \
72         }                                                                      \
73         __b;                                                                   \
74 })
75
76 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
77
78 // Return the integer logarithm of the value provided rounded down
79 static inline uintptr_t LOG2_DOWN(uintptr_t value)
80 {
81         value |= 1;  // clz(0) is undefined, just or in a 1 bit and define it
82         // intrinsic __builtin_clz supported by both > gcc4.6 and LLVM > 1.5
83         return (sizeof(value) == 8) ? 63 - __builtin_clzll(value)
84                                     : 31 - __builtin_clz(value);
85 }
86
87 // Return the integer logarithm of the value provided rounded up
88 static inline uintptr_t LOG2_UP(uintptr_t value)
89 {
90         uintptr_t ret = LOG2_DOWN(value);
91
92         // Add 1 if a lower bit set
93         ret += 0 != (value ^ ((uintptr_t) 1 << ret));
94         return ret;
95 }
96
97 static inline uintptr_t ROUNDUPPWR2(uintptr_t value)
98 {
99         return 1 << LOG2_UP(value);
100 }
101
102 static inline uintptr_t ROUNDDOWNPWR2(uintptr_t value)
103 {
104         return 1 << LOG2_DOWN(value);
105 }
106
107 /* We wraparound if UINT_MAX < a * b, which is also UINT_MAX / a < b. */
108 static inline bool mult_will_overflow_u64(uint64_t a, uint64_t b)
109 {
110         if (!a)
111                 return FALSE;
112         return (uint64_t)(-1) / a < b;
113 }
114
115 // Return the offset of 'member' relative to the beginning of a struct type
116 #ifndef offsetof
117 #define offsetof(type, member)  ((size_t) (&((type*)0)->member))
118 #endif
119
120 /* Return the container/struct holding the object 'ptr' points to */
121 #define container_of(ptr, type, member) ({                                     \
122         (type*)((char*)ptr - offsetof(type, member));                         \
123 })
124
125 /* Makes sure func is run exactly once.  Can handle concurrent callers, and
126  * other callers spin til the func is complete. */
127 #define run_once(func)                                                         \
128 do {                                                                           \
129         static bool ran_once = FALSE;                                          \
130         static bool is_running = FALSE;                                        \
131         if (!ran_once) {                                                       \
132                 /* fetch and set TRUE, w/o a header or test_and_set weirdness*/\
133                 if (!__sync_fetch_and_or(&is_running, TRUE)) {                 \
134                         /* we won the race and get to run the func */          \
135                         func;                                                  \
136                         /* don't let the ran_once write pass previous writes */\
137                         wmb();                                                 \
138                         ran_once = TRUE;                                       \
139                 } else {                                                       \
140                         /* someone else woni */                                \
141                         while (!ran_once)                                      \
142                                 cpu_relax();                                   \
143                 }                                                              \
144         }                                                                      \
145 } while (0)
146
147 /* Unprotected, single-threaded version, makes sure func is run exactly once */
148 #define run_once_racy(func)                                                    \
149 do {                                                                           \
150         static bool ran_once = FALSE;                                          \
151         if (!ran_once) {                                                       \
152                 func;                                                          \
153                 ran_once = TRUE;                                               \
154         }                                                                      \
155 } while (0)
156
157 #ifdef ROS_KERNEL
158 #include <compiler.h>
159 #else
160 /* Force the reading/writing exactly once of x.  You may still need mbs().  See
161  * http://lwn.net/Articles/508991/ for more info. */
162 #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
163 #define READ_ONCE(x) ACCESS_ONCE(x)
164 #define WRITE_ONCE(x, val) ((*(volatile typeof(x) *)&(x)) = val)
165 #endif
166
167 #endif /* __ASSEMBLER__ */