1 #ifndef __LIB_H__
2 #define __LIB_H__
3 
4 #define ROUNDUP(x, a) (((x) + (a) - 1) & ~((a) - 1))
5 
6 #define DIV_ROUND(n, d) (((n) + (d) / 2) / (d))
7 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
8 
9 #define MASK_EXTR(v, m) (((v) & (m)) / ((m) & -(m)))
10 #define MASK_INSR(v, m) (((v) * ((m) & -(m))) & (m))
11 
12 #define count_args_(dot, a1, a2, a3, a4, a5, a6, a7, a8, x, ...) x
13 #define count_args(args...) \
14     count_args_(., ## args, 8, 7, 6, 5, 4, 3, 2, 1, 0)
15 
16 #ifndef __ASSEMBLY__
17 
18 #include <xen/inttypes.h>
19 #include <xen/stdarg.h>
20 #include <xen/types.h>
21 #include <xen/xmalloc.h>
22 #include <xen/string.h>
23 #include <asm/bug.h>
24 
25 #define BUG_ON(p)  do { if (unlikely(p)) BUG();  } while (0)
26 #define WARN_ON(p) do { if (unlikely(p)) WARN(); } while (0)
27 
28 /* All clang versions supported by Xen have _Static_assert. */
29 #if defined(__clang__) || \
30     (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
31 /* Force a compilation error if condition is true */
32 #define BUILD_BUG_ON(cond) ({ _Static_assert(!(cond), "!(" #cond ")"); })
33 
34 /* Force a compilation error if condition is true, but also produce a
35    result (of value 0 and type size_t), so the expression can be used
36    e.g. in a structure initializer (or where-ever else comma expressions
37    aren't permitted). */
38 #define BUILD_BUG_ON_ZERO(cond) \
39     sizeof(struct { _Static_assert(!(cond), "!(" #cond ")"); })
40 #else
41 #define BUILD_BUG_ON_ZERO(cond) sizeof(struct { int:-!!(cond); })
42 #define BUILD_BUG_ON(cond) ((void)BUILD_BUG_ON_ZERO(cond))
43 #endif
44 
45 #ifdef CONFIG_GCOV
46 #define gcov_string "gcov=y"
47 #else
48 #define gcov_string ""
49 #endif
50 
51 #ifndef NDEBUG
52 #define ASSERT(p) \
53     do { if ( unlikely(!(p)) ) assert_failed(#p); } while (0)
54 #define ASSERT_UNREACHABLE() assert_failed("unreachable")
55 #define debug_build() 1
56 #else
57 #define ASSERT(p) do { if ( 0 && (p) ) {} } while (0)
58 #define ASSERT_UNREACHABLE() do { } while (0)
59 #define debug_build() 0
60 #endif
61 
62 #define ABS(_x) ({                              \
63     typeof(_x) __x = (_x);                      \
64     (__x < 0) ? -__x : __x;                     \
65 })
66 
67 #define SWAP(_a, _b) \
68    do { typeof(_a) _t = (_a); (_a) = (_b); (_b) = _t; } while ( 0 )
69 
70 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x))
71 
72 #define __ACCESS_ONCE(x) ({                             \
73             (void)(typeof(x))0; /* Scalar typecheck. */ \
74             (volatile typeof(x) *)&(x); })
75 #define ACCESS_ONCE(x) (*__ACCESS_ONCE(x))
76 
77 struct domain;
78 
79 void cmdline_parse(const char *cmdline);
80 int parse_bool(const char *s, const char *e);
81 
82 /**
83  * Given a specific name, parses a string of the form:
84  *   [no-]$NAME[=...]
85  * returning 0 or 1 for a recognised boolean, or -1 for an error.
86  */
87 int parse_boolean(const char *name, const char *s, const char *e);
88 
89 /**
90  * Very similar to strcmp(), but will declare a match if the NUL in 'name'
91  * lines up with comma, colon, semicolon or equals in 'frag'.  Designed for
92  * picking exact string matches out of a delimited command line list.
93  */
94 int cmdline_strcmp(const char *frag, const char *name);
95 
96 #ifdef CONFIG_DEBUG_TRACE
97 extern void debugtrace_dump(void);
98 extern void debugtrace_printk(const char *fmt, ...)
99     __attribute__ ((format (printf, 1, 2)));
100 #else
debugtrace_dump(void)101 static inline void debugtrace_dump(void) {}
102 static inline void
103  __attribute__ ((format (printf, 1, 2)))
debugtrace_printk(const char * fmt,...)104 debugtrace_printk(const char *fmt, ...) {}
105 #endif
106 
107 /* Allows us to use '%p' as general-purpose machine-word format char. */
108 #define _p(_x) ((void *)(unsigned long)(_x))
109 extern void printk(const char *format, ...)
110     __attribute__ ((format (printf, 1, 2)));
111 
112 #define printk_once(fmt, args...)               \
113 ({                                              \
114     static bool __read_mostly once_;            \
115     if ( unlikely(!once_) )                     \
116     {                                           \
117         once_ = true;                           \
118         printk(fmt, ## args);                   \
119     }                                           \
120 })
121 
122 extern void guest_printk(const struct domain *d, const char *format, ...)
123     __attribute__ ((format (printf, 2, 3)));
124 extern void noreturn panic(const char *format, ...)
125     __attribute__ ((format (printf, 1, 2)));
126 extern int __printk_ratelimit(int ratelimit_ms, int ratelimit_burst);
127 extern int printk_ratelimit(void);
128 
129 #define gprintk(lvl, fmt, args...) \
130     printk(XENLOG_GUEST lvl "%pv " fmt, current, ## args)
131 
132 #ifdef NDEBUG
133 
134 static inline void
135 __attribute__ ((__format__ (__printf__, 2, 3)))
dprintk(const char * lvl,const char * fmt,...)136 dprintk(const char *lvl, const char *fmt, ...) {}
137 
138 static inline void
139 __attribute__ ((__format__ (__printf__, 2, 3)))
gdprintk(const char * lvl,const char * fmt,...)140 gdprintk(const char *lvl, const char *fmt, ...) {}
141 
142 #else
143 
144 #define dprintk(lvl, fmt, args...) \
145     printk(lvl "%s:%d: " fmt, __FILE__, __LINE__, ## args)
146 #define gdprintk(lvl, fmt, args...) \
147     printk(XENLOG_GUEST lvl "%s:%d:%pv " fmt, \
148            __FILE__, __LINE__, current, ## args)
149 
150 #endif
151 
152 /* vsprintf.c */
153 #define sprintf __xen_has_no_sprintf__
154 #define vsprintf __xen_has_no_vsprintf__
155 extern int snprintf(char * buf, size_t size, const char * fmt, ...)
156     __attribute__ ((format (printf, 3, 4)));
157 extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
158     __attribute__ ((format (printf, 3, 0)));
159 extern int scnprintf(char * buf, size_t size, const char * fmt, ...)
160     __attribute__ ((format (printf, 3, 4)));
161 extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
162     __attribute__ ((format (printf, 3, 0)));
163 extern int asprintf(char ** bufp, const char * fmt, ...)
164     __attribute__ ((format (printf, 2, 3)));
165 extern int vasprintf(char ** bufp, const char * fmt, va_list args)
166     __attribute__ ((format (printf, 2, 0)));
167 
168 long simple_strtol(
169     const char *cp,const char **endp, unsigned int base);
170 unsigned long simple_strtoul(
171     const char *cp,const char **endp, unsigned int base);
172 long long simple_strtoll(
173     const char *cp,const char **endp, unsigned int base);
174 unsigned long long simple_strtoull(
175     const char *cp,const char **endp, unsigned int base);
176 
177 unsigned long long parse_size_and_unit(const char *s, const char **ps);
178 
179 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c);
180 
181 #define TAINT_SYNC_CONSOLE              (1u << 0)
182 #define TAINT_MACHINE_CHECK             (1u << 1)
183 #define TAINT_ERROR_INJECT              (1u << 2)
184 #define TAINT_HVM_FEP                   (1u << 3)
185 extern unsigned int tainted;
186 #define TAINT_STRING_MAX_LEN            20
187 extern char *print_tainted(char *str);
188 extern void add_taint(unsigned int taint);
189 
190 struct cpu_user_regs;
191 void dump_execstate(struct cpu_user_regs *);
192 
193 void init_constructors(void);
194 
195 void *bsearch(const void *key, const void *base, size_t num, size_t size,
196               int (*cmp)(const void *key, const void *elt));
197 
198 #endif /* __ASSEMBLY__ */
199 
200 #endif /* __LIB_H__ */
201