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