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