xref: /kvm-unit-tests/lib/libcflat.h (revision 7b18fa38237a5f78af413a2efb78da36e7182f14)
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 #ifndef __ASSEMBLY__
24 
25 #include <linux/compiler.h>
26 #include <stdarg.h>
27 #include <stddef.h>
28 #include <stdint.h>
29 #include <string.h>
30 #include <stdbool.h>
31 
32 #define xstr(s...) xxstr(s)
33 #define xxstr(s...) #s
34 
35 #define __ALIGN_MASK(x, mask)	(((x) + (mask)) & ~(mask))
36 #define __ALIGN(x, a)		__ALIGN_MASK(x, (typeof(x))(a) - 1)
37 #define ALIGN(x, a)		__ALIGN((x), (a))
38 #define IS_ALIGNED(x, a)	(((x) & ((typeof(x))(a) - 1)) == 0)
39 
40 #define MIN(a, b)		((a) < (b) ? (a) : (b))
41 #define MAX(a, b)		((a) > (b) ? (a) : (b))
42 
43 typedef uint8_t		u8;
44 typedef int8_t		s8;
45 typedef uint16_t	u16;
46 typedef int16_t		s16;
47 typedef uint32_t	u32;
48 typedef int32_t		s32;
49 typedef uint64_t	u64;
50 typedef int64_t		s64;
51 typedef unsigned long	ulong;
52 
53 #if __SIZEOF_LONG__ == 8
54 #  define __PRI32_PREFIX
55 #  define __PRI64_PREFIX	"l"
56 #  define __PRIPTR_PREFIX	"l"
57 #else
58 #if defined(__U32_LONG_FMT__)
59 #  define __PRI32_PREFIX        "l"
60 #else
61 #  define __PRI32_PREFIX
62 #endif
63 #  define __PRI64_PREFIX	"ll"
64 #  define __PRIPTR_PREFIX
65 #endif
66 #define PRId32  __PRI32_PREFIX	"d"
67 #define PRIu32  __PRI32_PREFIX	"u"
68 #define PRIx32  __PRI32_PREFIX	"x"
69 #define PRId64  __PRI64_PREFIX	"d"
70 #define PRIu64  __PRI64_PREFIX	"u"
71 #define PRIx64  __PRI64_PREFIX	"x"
72 #define PRIxPTR __PRIPTR_PREFIX	"x"
73 
74 typedef u64			phys_addr_t;
75 #define INVALID_PHYS_ADDR	(~(phys_addr_t)0)
76 
77 extern void puts(const char *s);
78 extern int __getchar(void);
79 extern int getchar(void);
80 extern void exit(int code) __attribute__((noreturn));
81 extern void abort(void) __attribute__((noreturn));
82 extern long atol(const char *ptr);
83 extern char *getenv(const char *name);
84 
85 extern int printf(const char *fmt, ...)
86 					__attribute__((format(printf, 1, 2)));
87 extern int snprintf(char *buf, int size, const char *fmt, ...)
88 					__attribute__((format(printf, 3, 4)));
89 extern int vsnprintf(char *buf, int size, const char *fmt, va_list va)
90 					__attribute__((format(printf, 3, 0)));
91 extern int vprintf(const char *fmt, va_list va)
92 					__attribute__((format(printf, 1, 0)));
93 
94 void report_prefix_pushf(const char *prefix_fmt, ...)
95 					__attribute__((format(printf, 1, 2)));
96 extern void report_prefix_push(const char *prefix);
97 extern void report_prefix_pop(void);
98 extern void report(bool pass, const char *msg_fmt, ...)
99 		__attribute__((format(printf, 2, 3), nonnull(2)));
100 extern void report_xfail(bool xfail, bool pass, const char *msg_fmt, ...)
101 		__attribute__((format(printf, 3, 4), nonnull(3)));
102 extern void report_abort(const char *msg_fmt, ...)
103 					__attribute__((format(printf, 1, 2)))
104 					__attribute__((noreturn));
105 extern void report_skip(const char *msg_fmt, ...)
106 					__attribute__((format(printf, 1, 2)));
107 extern void report_info(const char *msg_fmt, ...)
108 					__attribute__((format(printf, 1, 2)));
109 extern void report_pass(const char *msg_fmt, ...)
110 					__attribute__((format(printf, 1, 2)));
111 extern void report_fail(const char *msg_fmt, ...)
112 					__attribute__((format(printf, 1, 2)));
113 extern void report_passed(void);
114 extern int report_summary(void);
115 
116 bool simple_glob(const char *text, const char *pattern);
117 
118 extern void dump_stack(void);
119 extern void dump_frame_stack(const void *instruction, const void *frame);
120 
121 #define ARRAY_SIZE(_a) (sizeof(_a)/sizeof((_a)[0]))
122 
123 #define container_of(ptr, type, member) ({				\
124 	const typeof( ((type *)0)->member ) *__mptr = (ptr);		\
125 	(type *)( (char *)__mptr - offsetof(type,member) );})
126 
127 #define assert(cond)							\
128 do {									\
129 	if (!(cond)) {							\
130 		printf("%s:%d: assert failed: %s\n",			\
131 		       __FILE__, __LINE__, #cond);			\
132 		dump_stack();						\
133 		abort();						\
134 	}								\
135 } while (0)
136 
137 #define assert_msg(cond, fmt, args...)					\
138 do {									\
139 	if (!(cond)) {							\
140 		printf("%s:%d: assert failed: %s: " fmt "\n",		\
141 		       __FILE__, __LINE__, #cond, ## args);		\
142 		dump_stack();						\
143 		abort();						\
144 	}								\
145 } while (0)
146 
147 /*
148  * One byte per bit, a ' between each group of 4 bits, and a null terminator.
149  */
150 #define BINSTR_SZ (sizeof(unsigned long) * 8 + sizeof(unsigned long) * 2)
151 void binstr(unsigned long x, char out[BINSTR_SZ]);
152 void print_binstr(unsigned long x);
153 
154 extern void setup_vm(void);
155 
156 #endif /* !__ASSEMBLY__ */
157 
158 #define SZ_256			(1 << 8)
159 #define SZ_4K			(1 << 12)
160 #define SZ_8K			(1 << 13)
161 #define SZ_16K			(1 << 14)
162 #define SZ_64K			(1 << 16)
163 #define SZ_1M			(1 << 20)
164 #define SZ_2M			(1 << 21)
165 #define SZ_1G			(1 << 30)
166 #define SZ_2G			(1ul << 31)
167 
168 #endif
169