Always define macro MODE16 - that way it can be used in C conditionals.
[seabios.git] / src / farptr.h
1 // Code to access multiple segments within gcc.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU GPLv3 license.
6 #ifndef __FARPTR_H
7 #define __FARPTR_H
8
9 #include "ioport.h" // insb
10
11 // Dummy definitions used to make sure gcc understands dependencies
12 // between SET_SEG and GET/READ/WRITE_SEG macros.
13 extern u16 __segment_ES, __segment_CS, __segment_DS, __segment_SS;
14
15 // Low level macros for reading/writing memory via a segment selector.
16 #define READ8_SEG(SEG, var) ({                          \
17     typeof(var) __value;                                \
18     __asm__("movb %%" #SEG ":%1, %b0" : "=Qi"(__value)  \
19             : "m"(var), "m"(__segment_ ## SEG));        \
20     __value; })
21 #define READ16_SEG(SEG, var) ({                         \
22     typeof(var) __value;                                \
23     __asm__("movw %%" #SEG ":%1, %w0" : "=ri"(__value)  \
24             : "m"(var), "m"(__segment_ ## SEG));        \
25     __value; })
26 #define READ32_SEG(SEG, var) ({                         \
27     typeof(var) __value;                                \
28     __asm__("movl %%" #SEG ":%1, %0" : "=ri"(__value)   \
29             : "m"(var), "m"(__segment_ ## SEG));        \
30     __value; })
31 #define READ64_SEG(SEG, var) ({                                 \
32     union u64_u32_u __value;                                    \
33     union u64_u32_u *__r64_ptr = (union u64_u32_u *)&(var);     \
34     __value.hi = READ32_SEG(SEG, __r64_ptr->hi);                \
35     __value.lo = READ32_SEG(SEG, __r64_ptr->lo);                \
36     __value.val; })
37 #define WRITE8_SEG(SEG, var, value)                             \
38     __asm__("movb %b1, %%" #SEG ":%0" : "=m"(var)               \
39             : "Q"(value), "m"(__segment_ ## SEG))
40 #define WRITE16_SEG(SEG, var, value)                            \
41     __asm__("movw %w1, %%" #SEG ":%0" : "=m"(var)               \
42             : "r"(value), "m"(__segment_ ## SEG))
43 #define WRITE32_SEG(SEG, var, value)                            \
44     __asm__("movl %1, %%" #SEG ":%0" : "=m"(var)                \
45             : "r"(value), "m"(__segment_ ## SEG))
46 #define WRITE64_SEG(SEG, var, value) do {                       \
47         union u64_u32_u __value;                                \
48         union u64_u32_u *__w64_ptr = (union u64_u32_u *)&(var); \
49         __value.val = (value);                                  \
50         WRITE32_SEG(SEG, __w64_ptr->hi, __value.hi);            \
51         WRITE32_SEG(SEG, __w64_ptr->lo, __value.lo);            \
52     } while (0)
53
54 // Low level macros for getting/setting a segment register.
55 #define __SET_SEG(SEG, value)                                   \
56     __asm__("movw %w1, %%" #SEG : "=m"(__segment_ ## SEG)       \
57             : "r"(value))
58 #define __GET_SEG(SEG) ({                                       \
59     u16 __seg;                                                  \
60     __asm__("movw %%" #SEG ", %w0" : "=r"(__seg)                \
61             : "m"(__segment_ ## SEG));                          \
62     __seg;})
63
64 // Macros for automatically choosing the appropriate memory size
65 // access method.
66 extern void __force_link_error__unknown_type();
67
68 #define __GET_VAR(seg, var) ({                                          \
69     typeof(var) __val;                                                  \
70     if (__builtin_types_compatible_p(typeof(__val), u8)                 \
71         || __builtin_types_compatible_p(typeof(__val), s8))             \
72         __val = READ8_SEG(seg, var);                                    \
73     else if (__builtin_types_compatible_p(typeof(__val), u16)           \
74              || __builtin_types_compatible_p(typeof(__val), s16))       \
75         __val = READ16_SEG(seg, var);                                   \
76     else if (__builtin_types_compatible_p(typeof(__val), u32)           \
77              || __builtin_types_compatible_p(typeof(__val), s32))       \
78         __val = READ32_SEG(seg, var);                                   \
79     else if (__builtin_types_compatible_p(typeof(__val), u64)           \
80              || __builtin_types_compatible_p(typeof(__val), s64))       \
81         __val = READ64_SEG(seg, var);                                   \
82     else                                                                \
83         __force_link_error__unknown_type();                             \
84     __val; })
85
86 #define __SET_VAR(seg, var, val) do {                                   \
87         if (__builtin_types_compatible_p(typeof(var), u8)               \
88             || __builtin_types_compatible_p(typeof(var), s8))           \
89             WRITE8_SEG(seg, var, (val));                                \
90         else if (__builtin_types_compatible_p(typeof(var), u16)         \
91                  || __builtin_types_compatible_p(typeof(var), s16))     \
92             WRITE16_SEG(seg, var, (val));                               \
93         else if (__builtin_types_compatible_p(typeof(var), u32)         \
94                  || __builtin_types_compatible_p(typeof(var), s32))     \
95             WRITE32_SEG(seg, var, (val));                               \
96         else if (__builtin_types_compatible_p(typeof(var), u64)         \
97                  || __builtin_types_compatible_p(typeof(var), s64))     \
98             WRITE64_SEG(seg, var, (val));                               \
99         else                                                            \
100             __force_link_error__unknown_type();                         \
101     } while (0)
102
103 // Macros for accessing a variable in another segment.  (They
104 // automatically update the %es segment and then make the appropriate
105 // access.)
106 #define __GET_FARVAR(seg, var) ({               \
107     SET_SEG(ES, (seg));                         \
108     GET_VAR(ES, (var)); })
109 #define __SET_FARVAR(seg, var, val) do {        \
110         typeof(var) __sfv_val = (val);          \
111         SET_SEG(ES, (seg));                     \
112         SET_VAR(ES, (var), __sfv_val);          \
113     } while (0)
114
115 // Macros for accesssing a 32bit pointer from 16bit mode.  (They
116 // automatically update the %es segment, break the pointer into
117 // segment/offset, and then make the access.)
118 #define __GET_FARPTR(ptr) ({                                            \
119     typeof(&(ptr)) __ptr = &(ptr);                                      \
120     GET_FARVAR(FARPTR_TO_SEG(__ptr)                                     \
121                , *(typeof(__ptr))FARPTR_TO_OFFSET(__ptr)); })
122 #define __SET_FARPTR(ptr, val) do {                                     \
123         typeof (&(ptr)) __ptr = &(ptr);                                 \
124         SET_FARVAR(FARPTR_TO_SEG(__ptr)                                 \
125                    , *(typeof(__ptr))FARPTR_TO_OFFSET(__ptr)            \
126                    , (val));                                            \
127     } while (0)
128
129 // Macros for converting to/from 32bit style pointers to their
130 // equivalent 16bit segment/offset values.
131 #define FARPTR_TO_SEG(p) (((u32)(p)) >> 4)
132 #define FARPTR_TO_OFFSET(p) (((u32)(p)) & 0xf)
133 #define MAKE_FARPTR(seg,off) ((void*)(((seg)<<4)+(off)))
134
135
136 #if MODE16 == 1
137
138 // Definitions when in 16 bit mode.
139 #define GET_FARVAR(seg, var) __GET_FARVAR((seg), (var))
140 #define SET_FARVAR(seg, var, val) __SET_FARVAR((seg), (var), (val))
141 #define GET_VAR(seg, var) __GET_VAR(seg, (var))
142 #define SET_VAR(seg, var, val) __SET_VAR(seg, (var), (val))
143 #define SET_SEG(SEG, value) __SET_SEG(SEG, (value))
144 #define GET_SEG(SEG) __GET_SEG(SEG)
145 #define GET_FARPTR(ptr) __GET_FARPTR(ptr)
146 #define SET_FARPTR(ptr, val) __SET_FARPTR((ptr), (val))
147
148 static inline void insb_far(u16 port, void *farptr, u16 count) {
149     SET_SEG(ES, FARPTR_TO_SEG(farptr));
150     insb(port, (u8*)FARPTR_TO_OFFSET(farptr), count);
151 }
152 static inline void insw_far(u16 port, void *farptr, u16 count) {
153     SET_SEG(ES, FARPTR_TO_SEG(farptr));
154     insw(port, (u16*)FARPTR_TO_OFFSET(farptr), count);
155 }
156 static inline void insl_far(u16 port, void *farptr, u16 count) {
157     SET_SEG(ES, FARPTR_TO_SEG(farptr));
158     insl(port, (u32*)FARPTR_TO_OFFSET(farptr), count);
159 }
160 static inline void outsb_far(u16 port, void *farptr, u16 count) {
161     SET_SEG(ES, FARPTR_TO_SEG(farptr));
162     outsb(port, (u8*)FARPTR_TO_OFFSET(farptr), count);
163 }
164 static inline void outsw_far(u16 port, void *farptr, u16 count) {
165     SET_SEG(ES, FARPTR_TO_SEG(farptr));
166     outsw(port, (u16*)FARPTR_TO_OFFSET(farptr), count);
167 }
168 static inline void outsl_far(u16 port, void *farptr, u16 count) {
169     SET_SEG(ES, FARPTR_TO_SEG(farptr));
170     outsl(port, (u32*)FARPTR_TO_OFFSET(farptr), count);
171 }
172
173 #else
174
175 // In 32-bit mode there is no need to mess with the segments.
176 #define GET_FARVAR(seg, var) \
177     (*((typeof(&(var)))MAKE_FARPTR((seg), (u32)&(var))))
178 #define SET_FARVAR(seg, var, val) \
179     do { GET_FARVAR((seg), (var)) = (val); } while (0)
180 #define GET_VAR(seg, var) (var)
181 #define SET_VAR(seg, var, val) do { (var) = (val); } while (0)
182 #define SET_SEG(SEG, value) ((void)(value))
183 #define GET_SEG(SEG) 0
184 #define GET_FARPTR(ptr) (ptr)
185 #define SET_FARPTR(ptr, val) do { (ptr) = (val); } while (0)
186
187 #define insb_far(port, farptr, count) insb(port, farptr, count)
188 #define insw_far(port, farptr, count) insw(port, farptr, count)
189 #define insl_far(port, farptr, count) insl(port, farptr, count)
190 #define outsb_far(port, farptr, count) outsb(port, farptr, count)
191 #define outsw_far(port, farptr, count) outsw(port, farptr, count)
192 #define outsl_far(port, farptr, count) outsl(port, farptr, count)
193
194 #endif
195
196 #endif // farptr.h