xref: /kvm-unit-tests/lib/libcflat.h (revision a9d450af71776366f41cfa76aa5070eda59caef4)
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(void);
110 extern int report_summary(void);
111 
112 bool simple_glob(const char *text, const char *pattern);
113 
114 extern void dump_stack(void);
115 extern void dump_frame_stack(const void *instruction, const void *frame);
116 
117 #define ARRAY_SIZE(_a) (sizeof(_a)/sizeof((_a)[0]))
118 
119 #define container_of(ptr, type, member) ({				\
120 	const typeof( ((type *)0)->member ) *__mptr = (ptr);		\
121 	(type *)( (char *)__mptr - offsetof(type,member) );})
122 
123 #define assert(cond)							\
124 do {									\
125 	if (!(cond)) {							\
126 		printf("%s:%d: assert failed: %s\n",			\
127 		       __FILE__, __LINE__, #cond);			\
128 		dump_stack();						\
129 		abort();						\
130 	}								\
131 } while (0)
132 
133 #define assert_msg(cond, fmt, args...)					\
134 do {									\
135 	if (!(cond)) {							\
136 		printf("%s:%d: assert failed: %s: " fmt "\n",		\
137 		       __FILE__, __LINE__, #cond, ## args);		\
138 		dump_stack();						\
139 		abort();						\
140 	}								\
141 } while (0)
142 
143 /*
144  * One byte per bit, a ' between each group of 4 bits, and a null terminator.
145  */
146 #define BINSTR_SZ (sizeof(unsigned long) * 8 + sizeof(unsigned long) * 2)
147 void binstr(unsigned long x, char out[BINSTR_SZ]);
148 void print_binstr(unsigned long x);
149 
150 extern void setup_vm(void);
151 
152 #endif /* !__ASSEMBLY__ */
153 
154 #define SZ_256			(1 << 8)
155 #define SZ_4K			(1 << 12)
156 #define SZ_8K			(1 << 13)
157 #define SZ_16K			(1 << 14)
158 #define SZ_64K			(1 << 16)
159 #define SZ_1M			(1 << 20)
160 #define SZ_2M			(1 << 21)
161 #define SZ_1G			(1 << 30)
162 #define SZ_2G			(1ul << 31)
163 
164 #endif
165