xref: /kvm-unit-tests/lib/libcflat.h (revision 4a18bde1663489b27e679142e56809c1fd49b90d)
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License, version 2, as
4  * published by the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software
13  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
14  *
15  * Copyright IBM Corp. 2008
16  *
17  * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18  */
19 
20 #ifndef __LIBCFLAT_H
21 #define __LIBCFLAT_H
22 
23 #include <stdarg.h>
24 #include <stddef.h>
25 #include <stdint.h>
26 #include <string.h>
27 
28 #define __unused __attribute__((__unused__))
29 
30 #define xstr(s...) xxstr(s)
31 #define xxstr(s...) #s
32 
33 #define __ALIGN_MASK(x, mask)	(((x) + (mask)) & ~(mask))
34 #define __ALIGN(x, a)		__ALIGN_MASK(x, (typeof(x))(a) - 1)
35 #define ALIGN(x, a)		__ALIGN((x), (a))
36 #define IS_ALIGNED(x, a)	(((x) & ((typeof(x))(a) - 1)) == 0)
37 
38 #define SZ_4K			(1 << 12)
39 #define SZ_64K			(1 << 16)
40 #define SZ_2M			(1 << 21)
41 #define SZ_1G			(1 << 30)
42 
43 #define MIN(a, b)		((a) < (b) ? (a) : (b))
44 #define MAX(a, b)		((a) > (b) ? (a) : (b))
45 
46 typedef uint8_t		u8;
47 typedef int8_t		s8;
48 typedef uint16_t	u16;
49 typedef int16_t		s16;
50 typedef uint32_t	u32;
51 typedef int32_t		s32;
52 typedef uint64_t	u64;
53 typedef int64_t		s64;
54 typedef unsigned long	ulong;
55 
56 typedef _Bool		bool;
57 #define false 0
58 #define true  1
59 
60 #if __SIZEOF_LONG__ == 8
61 #  define __PRI32_PREFIX
62 #  define __PRI64_PREFIX	"l"
63 #  define __PRIPTR_PREFIX	"l"
64 #else
65 #if defined(__U32_LONG_FMT__)
66 #  define __PRI32_PREFIX        "l"
67 #else
68 #  define __PRI32_PREFIX
69 #endif
70 #  define __PRI64_PREFIX	"ll"
71 #  define __PRIPTR_PREFIX
72 #endif
73 #define PRId32  __PRI32_PREFIX	"d"
74 #define PRIu32  __PRI32_PREFIX	"u"
75 #define PRIx32  __PRI32_PREFIX	"x"
76 #define PRId64  __PRI64_PREFIX	"d"
77 #define PRIu64  __PRI64_PREFIX	"u"
78 #define PRIx64  __PRI64_PREFIX	"x"
79 #define PRIxPTR __PRIPTR_PREFIX	"x"
80 
81 typedef u64			phys_addr_t;
82 #define INVALID_PHYS_ADDR	(~(phys_addr_t)0)
83 
84 extern void puts(const char *s);
85 extern void exit(int code);
86 extern void abort(void);
87 extern long atol(const char *ptr);
88 extern char *getenv(const char *name);
89 
90 extern int printf(const char *fmt, ...)
91 					__attribute__((format(printf, 1, 2)));
92 extern int snprintf(char *buf, int size, const char *fmt, ...)
93 					__attribute__((format(printf, 3, 4)));
94 extern int vsnprintf(char *buf, int size, const char *fmt, va_list va)
95 					__attribute__((format(printf, 3, 0)));
96 extern int vprintf(const char *fmt, va_list va)
97 					__attribute__((format(printf, 1, 0)));
98 
99 void report_prefix_pushf(const char *prefix_fmt, ...);
100 extern void report_prefix_push(const char *prefix);
101 extern void report_prefix_pop(void);
102 extern void report(const char *msg_fmt, bool pass, ...);
103 extern void report_xfail(const char *msg_fmt, bool xfail, bool pass, ...);
104 extern void report_abort(const char *msg_fmt, ...);
105 extern void report_skip(const char *msg_fmt, ...);
106 extern void report_info(const char *msg_fmt, ...);
107 extern void report_pass(void);
108 extern int report_summary(void);
109 
110 bool simple_glob(const char *text, const char *pattern);
111 
112 extern void dump_stack(void);
113 extern void dump_frame_stack(const void *instruction, const void *frame);
114 
115 #define ARRAY_SIZE(_a) (sizeof(_a)/sizeof((_a)[0]))
116 
117 #define container_of(ptr, type, member) ({				\
118 	const typeof( ((type *)0)->member ) *__mptr = (ptr);		\
119 	(type *)( (char *)__mptr - offsetof(type,member) );})
120 
121 #define assert(cond)							\
122 do {									\
123 	if (!(cond)) {							\
124 		printf("%s:%d: assert failed: %s\n",			\
125 		       __FILE__, __LINE__, #cond);			\
126 		dump_stack();						\
127 		abort();						\
128 	}								\
129 } while (0)
130 
131 #define assert_msg(cond, fmt, args...)					\
132 do {									\
133 	if (!(cond)) {							\
134 		printf("%s:%d: assert failed: %s: " fmt "\n",		\
135 		       __FILE__, __LINE__, #cond, ## args);		\
136 		dump_stack();						\
137 		abort();						\
138 	}								\
139 } while (0)
140 
141 static inline bool is_power_of_2(unsigned long n)
142 {
143 	return n && !(n & (n - 1));
144 }
145 
146 /*
147  * One byte per bit, a ' between each group of 4 bits, and a null terminator.
148  */
149 #define BINSTR_SZ (sizeof(unsigned long) * 8 + sizeof(unsigned long) * 2)
150 void binstr(unsigned long x, char out[BINSTR_SZ]);
151 void print_binstr(unsigned long x);
152 
153 #endif
154