grml...
[seabios.git] / src / types.h
1 // Basic type definitions for X86 cpus.
2 //
3 // Copyright (C) 2008-2010  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
6 #ifndef __TYPES_H
7 #define __TYPES_H
8
9 typedef unsigned char u8;
10 typedef signed char s8;
11 typedef unsigned short u16;
12 typedef signed short s16;
13 typedef unsigned int u32;
14 typedef signed int s32;
15 typedef unsigned long long u64;
16 typedef signed long long s64;
17 typedef u32 size_t;
18
19 union u64_u32_u {
20     struct { u32 hi, lo; };
21     u64 val;
22 };
23
24 #ifdef MANUAL_NO_JUMP_TABLE
25 # define default case 775324556: asm(""); default
26 #endif
27
28 #ifdef WHOLE_PROGRAM
29 # define __VISIBLE __attribute__((externally_visible))
30 #else
31 # define __VISIBLE
32 #endif
33
34 #define UNIQSEC __FILE__ "." __stringify(__LINE__)
35
36 #define __noreturn __attribute__((noreturn))
37 extern void __force_link_error__only_in_32bit_flat(void) __noreturn;
38 extern void __force_link_error__only_in_32bit_segmented(void) __noreturn;
39 extern void __force_link_error__only_in_16bit(void) __noreturn;
40
41 #define __ASM(code) asm(".section .text.asm." UNIQSEC "\n\t" code)
42
43 #if MODE16 == 1
44 // Notes a function as externally visible in the 16bit code chunk.
45 # define VISIBLE16 __VISIBLE
46 // Notes a function as externally visible in the 32bit flat code chunk.
47 # define VISIBLE32FLAT __section(".discard.func32flat." UNIQSEC) noinline
48 // Notes a 32bit flat function that will only be called during init.
49 # define VISIBLE32INIT VISIBLE32FLAT
50 // Notes a function as externally visible in the 32bit segmented code chunk.
51 # define VISIBLE32SEG __section(".discard.func32seg." UNIQSEC) noinline
52 // Designate a variable as (only) visible to 16bit code.
53 # define VAR16 __section(".data16." UNIQSEC)
54 // Designate a variable as visible to 16bit, 32bit, and assembler code.
55 # define VAR16VISIBLE VAR16 __VISIBLE
56 // Designate a variable as externally visible (in addition to all internal code).
57 # define VAR16EXPORT __section(".data16.export." UNIQSEC) __VISIBLE
58 // Designate a variable at a specific 16bit address
59 # define VAR16FIXED(addr) __aligned(1) __VISIBLE __section(".fixedaddr." __stringify(addr))
60 // Designate a variable as (only) visible to 32bit segmented code.
61 # define VAR32SEG __section(".discard.var32seg." UNIQSEC)
62 // Designate a 32bit variable also available in 16bit "big real" mode.
63 # define VAR32FLATVISIBLE __section(".discard.var32flat." UNIQSEC) __VISIBLE __weak
64 // Designate top-level assembler as 16bit only.
65 # define ASM16(code) __ASM(code)
66 // Designate top-level assembler as 32bit flat only.
67 # define ASM32FLAT(code)
68 // Compile time check for a given mode.
69 # define ASSERT16() do { } while (0)
70 # define ASSERT32SEG() __force_link_error__only_in_32bit_segmented()
71 # define ASSERT32FLAT() __force_link_error__only_in_32bit_flat()
72 #elif MODESEGMENT == 1
73 # define VISIBLE16 __section(".discard.func16." UNIQSEC) noinline
74 # define VISIBLE32FLAT __section(".discard.func32flat." UNIQSEC) noinline
75 # define VISIBLE32INIT VISIBLE32FLAT
76 # define VISIBLE32SEG __VISIBLE
77 # define VAR16 __section(".discard.var16." UNIQSEC)
78 # define VAR16VISIBLE VAR16 __VISIBLE __weak
79 # define VAR16EXPORT VAR16VISIBLE
80 # define VAR16FIXED(addr) VAR16VISIBLE
81 # define VAR32SEG __section(".data32seg." UNIQSEC)
82 # define VAR32FLATVISIBLE __section(".discard.var32flat." UNIQSEC) __VISIBLE __weak
83 # define ASM16(code)
84 # define ASM32FLAT(code)
85 # define ASSERT16() __force_link_error__only_in_16bit()
86 # define ASSERT32SEG() do { } while (0)
87 # define ASSERT32FLAT() __force_link_error__only_in_32bit_flat()
88 #else
89 # define VISIBLE16 __section(".discard.func16." UNIQSEC) noinline
90 # define VISIBLE32FLAT __section(".text.runtime." UNIQSEC) __VISIBLE
91 # define VISIBLE32INIT __section(".text.init." UNIQSEC) __VISIBLE
92 # define VISIBLE32SEG __section(".discard.func32seg." UNIQSEC) noinline
93 # define VAR16 __section(".discard.var16." UNIQSEC)
94 # define VAR16VISIBLE VAR16 __VISIBLE __weak
95 # define VAR16EXPORT VAR16VISIBLE
96 # define VAR16FIXED(addr) VAR16VISIBLE
97 # define VAR32SEG __section(".discard.var32seg." UNIQSEC)
98 # define VAR32FLATVISIBLE __section(".data.runtime." UNIQSEC) __VISIBLE
99 # define ASM16(code)
100 # define ASM32FLAT(code) __ASM(code)
101 # define ASSERT16() __force_link_error__only_in_16bit()
102 # define ASSERT32SEG() __force_link_error__only_in_32bit_segmented()
103 # define ASSERT32FLAT() do { } while (0)
104 #endif
105
106 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
107 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
108 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
109 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
110 #define DIV_ROUND_CLOSEST(x, divisor)({                 \
111             typeof(divisor) __divisor = divisor;        \
112             (((x) + ((__divisor) / 2)) / (__divisor));  \
113         })
114 #define ALIGN(x,a)              __ALIGN_MASK(x,(typeof(x))(a)-1)
115 #define __ALIGN_MASK(x,mask)    (((x)+(mask))&~(mask))
116 #define ALIGN_DOWN(x,a)         ((x) & ~((typeof(x))(a)-1))
117 #define container_of(ptr, type, member) ({                      \
118         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
119         (type *)( (char *)__mptr - offsetof(type,member) );})
120
121 #define likely(x)       __builtin_expect(!!(x), 1)
122 #define unlikely(x)     __builtin_expect(!!(x), 0)
123
124 #define NULL ((void*)0)
125
126 #define __weak __attribute__((weak))
127 #define __section(S) __attribute__((section(S)))
128
129 #define PACKED __attribute__((packed))
130 #define __aligned(x) __attribute__((aligned(x)))
131
132 #define barrier() __asm__ __volatile__("": : :"memory")
133
134 #define noinline __attribute__((noinline))
135 #define __always_inline inline __attribute__((always_inline))
136 #define __malloc __attribute__((__malloc__))
137 #define __attribute_const __attribute__((__const__))
138
139 #define __stringify_1(x)        #x
140 #define __stringify(x)          __stringify_1(x)
141
142 #endif // types.h