Can vmap unaligned paddrs
[akaros.git] / kern / include / ros / common.h
index b600819..377e96d 100644 (file)
@@ -27,6 +27,17 @@ typedef unsigned long uintreg_t;
 #define FALSE  0
 #endif
 
+#define KiB    1024u
+#define MiB    1048576u
+#define GiB    1073741824u
+#define TiB    1099511627776ull
+#define PiB    1125899906842624ull
+#define EiB    1152921504606846976ull
+
+#define ALIGNED(p, a)  (!(((uintptr_t)(p)) & ((a)-1)))
+
+#define ARRAY_SIZE(x) (sizeof((x))/sizeof((x)[0]))
+
 #define CHECK_FLAG(flags,bit)   ((flags) & (1 << (bit)))
 
 #define FOR_CIRC_BUFFER(next, size, var) \
@@ -48,33 +59,38 @@ typedef unsigned long uintreg_t;
 })
 #endif
 
-// Rounding operations (efficient when n is a power of 2)
-// Round down to the nearest multiple of n
-#define ROUNDDOWN(a, n)                                                \
-({                                                             \
-       uintptr_t __a = (uintptr_t) (a);                                \
-       (typeof(a)) (__a - __a % (n));                          \
-})
-// Round up to the nearest multiple of n
-#define ROUNDUP(a, n)                                          \
-({                                                             \
-       uintptr_t __n = (uintptr_t) (n);                                \
-       (typeof(a)) (ROUNDDOWN((uintptr_t) (a) + __n - 1, __n));        \
+/* Rounding operations (efficient when n is a power of 2)
+ * Round down to the nearest multiple of n.
+ * The compiler should compile out the branch.  This is needed for 32 bit, so
+ * that we can round down uint64_t, without chopping off the top 32 bits. */
+#define ROUNDDOWN(a, n)                                                        \
+({                                                                             \
+       typeof(a) __b;                                                             \
+       if (sizeof(a) == 8) {                                                      \
+               uint64_t __a = (uint64_t) (a);                                         \
+               __b = (typeof(a)) (__a - __a % (n));                                   \
+       } else {                                                                   \
+               uintptr_t __a = (uintptr_t) (a);                                       \
+               __b = (typeof(a)) (__a - __a % (n));                                   \
+       }                                                                          \
+       __b;                                                                       \
 })
 
-// Round down to the nearest multiple of n
-#define PTRROUNDDOWN(a, n)                                             \
-({                                                             \
-       char * __a = (char *) (a);                              \
-       (typeof(a)) (__a - (uintptr_t)__a % (n));                               \
-})
-// Round pointer up to the nearest multiple of n
-#define PTRROUNDUP(a, n)                                               \
-({                                                             \
-       uintptr_t __n = (uintptr_t) (n);                                \
-       (typeof(a)) (PTRROUNDDOWN((char *) (a) + __n - 1, __n));        \
+/* Round up to the nearest multiple of n */
+#define ROUNDUP(a, n)                                                          \
+({                                                                             \
+       typeof(a) __b;                                                             \
+       if (sizeof(a) == 8) {                                                      \
+               uint64_t __n = (uint64_t) (n);                                         \
+               __b = (typeof(a)) (ROUNDDOWN((uint64_t) (a) + __n - 1, __n));          \
+       } else {                                                                   \
+               uintptr_t __n = (uintptr_t) (n);                                       \
+               __b = (typeof(a)) (ROUNDDOWN((uintptr_t) (a) + __n - 1, __n));         \
+       }                                                                          \
+       __b;                                                                       \
 })
 
+#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
 // Return the integer logarithm of the value provided rounded down
 static inline uintptr_t LOG2_DOWN(uintptr_t value)
 {
@@ -98,6 +114,11 @@ static inline uintptr_t ROUNDUPPWR2(uintptr_t value)
        return 1 << LOG2_UP(value);
 }
 
+static inline uintptr_t ROUNDDOWNPWR2(uintptr_t value)
+{
+       return 1 << LOG2_DOWN(value);
+}
+
 /* We wraparound if UINT_MAX < a * b, which is also UINT_MAX / a < b. */
 static inline bool mult_will_overflow_u64(uint64_t a, uint64_t b)
 {
@@ -120,10 +141,6 @@ static inline bool mult_will_overflow_u64(uint64_t a, uint64_t b)
  * http://lwn.net/Articles/508991/ for more info. */
 #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
 
-// Ivy currently can only handle 63 bits (OCaml thing), so use this to make
-// a uint64_t programatically
-#define UINT64(upper, lower) ( (((uint64_t)(upper)) << 32) | (lower) )
-
 /* Makes sure func is run exactly once.  Can handle concurrent callers, and
  * other callers spin til the func is complete. */
 #define run_once(func)                                                         \