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 #include <stdarg.h> 24 #include <stddef.h> 25 #include <stdint.h> 26 #include <string.h> 27 #include <stdbool.h> 28 29 #define __unused __attribute__((__unused__)) 30 31 #define xstr(s...) xxstr(s) 32 #define xxstr(s...) #s 33 34 #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask)) 35 #define __ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1) 36 #define ALIGN(x, a) __ALIGN((x), (a)) 37 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0) 38 39 #define SZ_4K (1 << 12) 40 #define SZ_64K (1 << 16) 41 #define SZ_2M (1 << 21) 42 #define SZ_1G (1 << 30) 43 44 #define MIN(a, b) ((a) < (b) ? (a) : (b)) 45 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 46 47 typedef uint8_t u8; 48 typedef int8_t s8; 49 typedef uint16_t u16; 50 typedef int16_t s16; 51 typedef uint32_t u32; 52 typedef int32_t s32; 53 typedef uint64_t u64; 54 typedef int64_t s64; 55 typedef unsigned long ulong; 56 57 #if __SIZEOF_LONG__ == 8 58 # define __PRI32_PREFIX 59 # define __PRI64_PREFIX "l" 60 # define __PRIPTR_PREFIX "l" 61 #else 62 #if defined(__U32_LONG_FMT__) 63 # define __PRI32_PREFIX "l" 64 #else 65 # define __PRI32_PREFIX 66 #endif 67 # define __PRI64_PREFIX "ll" 68 # define __PRIPTR_PREFIX 69 #endif 70 #define PRId32 __PRI32_PREFIX "d" 71 #define PRIu32 __PRI32_PREFIX "u" 72 #define PRIx32 __PRI32_PREFIX "x" 73 #define PRId64 __PRI64_PREFIX "d" 74 #define PRIu64 __PRI64_PREFIX "u" 75 #define PRIx64 __PRI64_PREFIX "x" 76 #define PRIxPTR __PRIPTR_PREFIX "x" 77 78 typedef u64 phys_addr_t; 79 #define INVALID_PHYS_ADDR (~(phys_addr_t)0) 80 81 extern void puts(const char *s); 82 extern int __getchar(void); 83 extern int getchar(void); 84 extern void exit(int code) __attribute__((noreturn)); 85 extern void abort(void) __attribute__((noreturn)); 86 extern long atol(const char *ptr); 87 extern char *getenv(const char *name); 88 89 extern int printf(const char *fmt, ...) 90 __attribute__((format(printf, 1, 2))); 91 extern int snprintf(char *buf, int size, const char *fmt, ...) 92 __attribute__((format(printf, 3, 4))); 93 extern int vsnprintf(char *buf, int size, const char *fmt, va_list va) 94 __attribute__((format(printf, 3, 0))); 95 extern int vprintf(const char *fmt, va_list va) 96 __attribute__((format(printf, 1, 0))); 97 98 void report_prefix_pushf(const char *prefix_fmt, ...) 99 __attribute__((format(printf, 1, 2))); 100 extern void report_prefix_push(const char *prefix); 101 extern void report_prefix_pop(void); 102 extern void report(const char *msg_fmt, bool pass, ...) 103 __attribute__((format(printf, 1, 3))); 104 extern void report_xfail(const char *msg_fmt, bool xfail, bool pass, ...) 105 __attribute__((format(printf, 1, 4))); 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(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 static inline bool is_power_of_2(unsigned long n) 148 { 149 return n && !(n & (n - 1)); 150 } 151 152 /* 153 * One byte per bit, a ' between each group of 4 bits, and a null terminator. 154 */ 155 #define BINSTR_SZ (sizeof(unsigned long) * 8 + sizeof(unsigned long) * 2) 156 void binstr(unsigned long x, char out[BINSTR_SZ]); 157 void print_binstr(unsigned long x); 158 159 extern void setup_vm(void); 160 161 #endif 162