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