b2100c1450c5ad44ee7b3b34e7877db210ed5319
[seabios.git] / src / types.h
1 // Basic type definitions for X86 cpus.
2 //
3 // Copyright (C) 2008,2009  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() __noreturn;
38 extern void __force_link_error__only_in_16bit() __noreturn;
39
40 #define __ASM(code) asm(".section .text.asm." UNIQSEC "\n\t" code)
41
42 #if MODE16 == 1
43 // Notes a function as externally visible in the 16bit code chunk.
44 # define VISIBLE16 __VISIBLE
45 // Notes a function as externally visible in the 32bit flat code chunk.
46 # define VISIBLE32FLAT
47 // Notes a function as externally visible in the 32bit segmented code chunk.
48 # define VISIBLE32SEG
49 // Designate a variable as (only) visible to 16bit code.
50 # define VAR16 __section(".data16." UNIQSEC)
51 // Designate a variable as visible to 16bit, 32bit, and assembler code.
52 # define VAR16VISIBLE VAR16 __VISIBLE
53 // Designate a variable as externally visible (in addition to all internal code).
54 # define VAR16EXPORT __section(".data16.export." UNIQSEC) __VISIBLE
55 // Designate a variable at a specific 16bit address
56 # define VAR16FIXED(addr) __aligned(1) __VISIBLE __section(".fixedaddr." __stringify(addr))
57 // Designate a variable as (only) visible to 32bit segmented code.
58 # define VAR32SEG __section(".discard.var32seg." UNIQSEC)
59 // Designate a 32bit variable also available in 16bit "big real" mode.
60 # define VAR32FLATVISIBLE __section(".discard.var32flat." UNIQSEC) __VISIBLE __weak
61 // Designate top-level assembler as 16bit only.
62 # define ASM16(code) __ASM(code)
63 // Designate top-level assembler as 32bit flat only.
64 # define ASM32FLAT(code)
65 // Compile time check for a given mode.
66 #define ASSERT16() do { } while (0)
67 #define ASSERT32FLAT() __force_link_error__only_in_32bit_flat()
68 #elif MODESEGMENT == 1
69 # define VISIBLE16
70 # define VISIBLE32FLAT
71 # define VISIBLE32SEG __VISIBLE
72 # define VAR16 __section(".discard.var16." UNIQSEC)
73 # define VAR16VISIBLE VAR16 __VISIBLE __weak
74 # define VAR16EXPORT VAR16VISIBLE
75 # define VAR16FIXED(addr) VAR16VISIBLE
76 # define VAR32SEG __section(".data32seg." UNIQSEC)
77 # define VAR32FLATVISIBLE __section(".discard.var32flat." UNIQSEC) __VISIBLE __weak
78 # define ASM16(code)
79 # define ASM32FLAT(code)
80 #define ASSERT16() __force_link_error__only_in_16bit()
81 #define ASSERT32FLAT() __force_link_error__only_in_32bit_flat()
82 #else
83 # define VISIBLE16
84 # define VISIBLE32FLAT __VISIBLE
85 # define VISIBLE32SEG
86 # define VAR16 __section(".discard.var16." UNIQSEC)
87 # define VAR16VISIBLE VAR16 __VISIBLE __weak
88 # define VAR16EXPORT VAR16VISIBLE
89 # define VAR16FIXED(addr) VAR16VISIBLE
90 # define VAR32SEG __section(".discard.var32seg." UNIQSEC)
91 # define VAR32FLATVISIBLE __VISIBLE
92 # define ASM16(code)
93 # define ASM32FLAT(code) __ASM(code)
94 #define ASSERT16() __force_link_error__only_in_16bit()
95 #define ASSERT32FLAT() do { } while (0)
96 #endif
97
98 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
99 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
100 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
101 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
102 #define DIV_ROUND_CLOSEST(x, divisor)({                 \
103             typeof(divisor) __divisor = divisor;        \
104             (((x) + ((__divisor) / 2)) / (__divisor));  \
105         })
106 #define ALIGN(x,a)              __ALIGN_MASK(x,(typeof(x))(a)-1)
107 #define __ALIGN_MASK(x,mask)    (((x)+(mask))&~(mask))
108 #define ALIGN_DOWN(x,a)         ((x) & ~((typeof(x))(a)-1))
109 #define container_of(ptr, type, member) ({                      \
110         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
111         (type *)( (char *)__mptr - offsetof(type,member) );})
112
113 #define NULL ((void*)0)
114
115 #define __weak __attribute__((weak))
116 #define __section(S) __attribute__((section(S)))
117
118 #define PACKED __attribute__((packed))
119 #define __aligned(x) __attribute__((aligned(x)))
120
121 #define barrier() __asm__ __volatile__("": : :"memory")
122
123 #define noinline __attribute__((noinline))
124 #define __always_inline inline __attribute__((always_inline))
125 #define __attribute_const __attribute__((__const__))
126
127 #define __stringify_1(x)        #x
128 #define __stringify(x)          __stringify_1(x)
129
130 #endif // types.h