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 28 #define __unused __attribute__((__unused__)) 29 30 #define xstr(s...) xxstr(s) 31 #define xxstr(s...) #s 32 33 #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask)) 34 #define __ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1) 35 #define ALIGN(x, a) __ALIGN((x), (a)) 36 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0) 37 38 #define SZ_4K (1 << 12) 39 #define SZ_64K (1 << 16) 40 #define SZ_2M (1 << 21) 41 #define SZ_1G (1 << 30) 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 typedef _Bool bool; 54 #define false 0 55 #define true 1 56 57 #if __SIZEOF_LONG__ == 8 58 # define __PRI64_PREFIX "l" 59 # define __PRIPTR_PREFIX "l" 60 #else 61 # define __PRI64_PREFIX "ll" 62 # define __PRIPTR_PREFIX 63 #endif 64 #define PRId64 __PRI64_PREFIX "d" 65 #define PRIu64 __PRI64_PREFIX "u" 66 #define PRIx64 __PRI64_PREFIX "x" 67 #define PRIxPTR __PRIPTR_PREFIX "x" 68 69 typedef u64 phys_addr_t; 70 #define INVALID_PHYS_ADDR (~(phys_addr_t)0) 71 72 extern void puts(const char *s); 73 extern void exit(int code); 74 extern void abort(void); 75 extern long atol(const char *ptr); 76 77 extern int printf(const char *fmt, ...) 78 __attribute__((format(printf, 1, 2))); 79 extern int snprintf(char *buf, int size, const char *fmt, ...) 80 __attribute__((format(printf, 3, 4))); 81 extern int vsnprintf(char *buf, int size, const char *fmt, va_list va) 82 __attribute__((format(printf, 3, 0))); 83 extern int vprintf(const char *fmt, va_list va) 84 __attribute__((format(printf, 1, 0))); 85 86 extern void report_prefix_push(const char *prefix); 87 extern void report_prefix_pop(void); 88 extern void report(const char *msg_fmt, bool pass, ...); 89 extern void report_xfail(const char *msg_fmt, bool xfail, bool pass, ...); 90 extern void report_abort(const char *msg_fmt, ...); 91 extern void report_skip(const char *msg_fmt, ...); 92 extern void report_info(const char *msg_fmt, ...); 93 extern int report_summary(void); 94 95 extern void dump_stack(void); 96 extern void dump_frame_stack(const void *instruction, const void *frame); 97 98 #define ARRAY_SIZE(_a) (sizeof(_a)/sizeof((_a)[0])) 99 100 #define container_of(ptr, type, member) ({ \ 101 const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 102 (type *)( (char *)__mptr - offsetof(type,member) );}) 103 104 #define assert(cond) \ 105 do { \ 106 if (!(cond)) { \ 107 printf("%s:%d: assert failed: %s\n", \ 108 __FILE__, __LINE__, #cond); \ 109 dump_stack(); \ 110 abort(); \ 111 } \ 112 } while (0) 113 114 static inline bool is_power_of_2(unsigned long n) 115 { 116 return n && !(n & (n - 1)); 117 } 118 119 #endif 120