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