360b28ee7dd30d0b41fb5c90f938ed6d87ebf011
[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 LGPLv3 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 extern u16 __segment_FS, __segment_GS;
15
16 // Low level macros for reading/writing memory via a segment selector.
17 #define READ8_SEG(SEG, value, var)                      \
18     __asm__("movb %%" #SEG ":%1, %b0" : "=Qi"(value)    \
19             : "m"(var), "m"(__segment_ ## SEG))
20 #define READ16_SEG(SEG, value, var)                     \
21     __asm__("movw %%" #SEG ":%1, %w0" : "=ri"(value)    \
22             : "m"(var), "m"(__segment_ ## SEG))
23 #define READ32_SEG(SEG, value, var)                     \
24     __asm__("movl %%" #SEG ":%1, %0" : "=ri"(value)     \
25             : "m"(var), "m"(__segment_ ## SEG))
26 #define READ64_SEG(SEG, value, var) do {                        \
27         union u64_u32_u __value;                                \
28         union u64_u32_u *__r64_ptr = (union u64_u32_u *)&(var); \
29         READ32_SEG(SEG, __value.hi, __r64_ptr->hi);             \
30         READ32_SEG(SEG, __value.lo, __r64_ptr->lo);             \
31         (value) = __value.val;                                  \
32     } while (0)
33 #define WRITE8_SEG(SEG, var, value)                             \
34     __asm__("movb %b1, %%" #SEG ":%0" : "=m"(var)               \
35             : "Q"(value), "m"(__segment_ ## SEG))
36 #define WRITE16_SEG(SEG, var, value)                            \
37     __asm__("movw %w1, %%" #SEG ":%0" : "=m"(var)               \
38             : "r"(value), "m"(__segment_ ## SEG))
39 #define WRITE32_SEG(SEG, var, value)                            \
40     __asm__("movl %1, %%" #SEG ":%0" : "=m"(var)                \
41             : "r"(value), "m"(__segment_ ## SEG))
42 #define WRITE64_SEG(SEG, var, value) do {                       \
43         union u64_u32_u __value;                                \
44         union u64_u32_u *__w64_ptr = (union u64_u32_u *)&(var); \
45         __value.val = (value);                                  \
46         WRITE32_SEG(SEG, __w64_ptr->hi, __value.hi);            \
47         WRITE32_SEG(SEG, __w64_ptr->lo, __value.lo);            \
48     } while (0)
49
50 // Low level macros for getting/setting a segment register.
51 #define __SET_SEG(SEG, value)                                   \
52     __asm__("movw %w1, %%" #SEG : "=m"(__segment_ ## SEG)       \
53             : "rm"(value))
54 #define __GET_SEG(SEG) ({                                       \
55     u16 __seg;                                                  \
56     __asm__("movw %%" #SEG ", %w0" : "=rm"(__seg)               \
57             : "m"(__segment_ ## SEG));                          \
58     __seg;})
59
60 // Macros for automatically choosing the appropriate memory size
61 // access method.
62 extern void __force_link_error__unknown_type();
63
64 #define __GET_VAR(seg, var) ({                  \
65     typeof(var) __val;                          \
66     if (sizeof(__val) == 1)                     \
67         READ8_SEG(seg, __val, var);             \
68     else if (sizeof(__val) == 2)                \
69         READ16_SEG(seg, __val, var);            \
70     else if (sizeof(__val) == 4)                \
71         READ32_SEG(seg, __val, var);            \
72     else if (sizeof(__val) == 8)                \
73         READ64_SEG(seg, __val, var);            \
74     else                                        \
75         __force_link_error__unknown_type();     \
76     __val; })
77
78 #define __SET_VAR(seg, var, val) do {           \
79         if (sizeof(var) == 1)                   \
80             WRITE8_SEG(seg, var, (val));        \
81         else if (sizeof(var) == 2)              \
82             WRITE16_SEG(seg, var, (val));       \
83         else if (sizeof(var) == 4)              \
84             WRITE32_SEG(seg, var, (val));       \
85         else if (sizeof(var) == 8)              \
86             WRITE64_SEG(seg, var, (val));       \
87         else                                    \
88             __force_link_error__unknown_type(); \
89     } while (0)
90
91 // Macros for accessing a variable in another segment.  (They
92 // automatically update the %es segment and then make the appropriate
93 // access.)
94 #define __GET_FARVAR(seg, var) ({               \
95     SET_SEG(ES, (seg));                         \
96     GET_VAR(ES, (var)); })
97 #define __SET_FARVAR(seg, var, val) do {        \
98         typeof(var) __sfv_val = (val);          \
99         SET_SEG(ES, (seg));                     \
100         SET_VAR(ES, (var), __sfv_val);          \
101     } while (0)
102
103 // Macros for accesssing a 32bit flat mode pointer from 16bit real
104 // mode.  (They automatically update the %es segment, break the
105 // pointer into segment/offset, and then make the access.)
106 #define __GET_FLATPTR(ptr) ({                                   \
107     typeof(&(ptr)) __ptr = &(ptr);                              \
108     GET_FARVAR(FLATPTR_TO_SEG(__ptr)                            \
109                , *(typeof(__ptr))FLATPTR_TO_OFFSET(__ptr)); })
110 #define __SET_FLATPTR(ptr, val) do {                            \
111         typeof (&(ptr)) __ptr = &(ptr);                         \
112         SET_FARVAR(FLATPTR_TO_SEG(__ptr)                        \
113                    , *(typeof(__ptr))FLATPTR_TO_OFFSET(__ptr)   \
114                    , (val));                                    \
115     } while (0)
116
117 // Macros for converting to/from 32bit flat mode pointers to their
118 // equivalent 16bit segment/offset values.
119 #define FLATPTR_TO_SEG(p) (((u32)(p)) >> 4)
120 #define FLATPTR_TO_OFFSET(p) (((u32)(p)) & 0xf)
121 #define MAKE_FLATPTR(seg,off) ((void*)(((u32)(seg)<<4)+(u32)(off)))
122
123
124 #if MODE16 == 1
125
126 // Definitions when in 16 bit mode.
127 #define GET_FARVAR(seg, var) __GET_FARVAR((seg), (var))
128 #define SET_FARVAR(seg, var, val) __SET_FARVAR((seg), (var), (val))
129 #define GET_VAR(seg, var) __GET_VAR(seg, (var))
130 #define SET_VAR(seg, var, val) __SET_VAR(seg, (var), (val))
131 #define SET_SEG(SEG, value) __SET_SEG(SEG, (value))
132 #define GET_SEG(SEG) __GET_SEG(SEG)
133 #define GET_FLATPTR(ptr) __GET_FLATPTR(ptr)
134 #define SET_FLATPTR(ptr, val) __SET_FLATPTR((ptr), (val))
135
136 static inline void insb_fl(u16 port, void *ptr_fl, u16 count) {
137     SET_SEG(ES, FLATPTR_TO_SEG(ptr_fl));
138     insb(port, (u8*)FLATPTR_TO_OFFSET(ptr_fl), count);
139 }
140 static inline void insw_fl(u16 port, void *ptr_fl, u16 count) {
141     SET_SEG(ES, FLATPTR_TO_SEG(ptr_fl));
142     insw(port, (u16*)FLATPTR_TO_OFFSET(ptr_fl), count);
143 }
144 static inline void insl_fl(u16 port, void *ptr_fl, u16 count) {
145     SET_SEG(ES, FLATPTR_TO_SEG(ptr_fl));
146     insl(port, (u32*)FLATPTR_TO_OFFSET(ptr_fl), count);
147 }
148 static inline void outsb_fl(u16 port, void *ptr_fl, u16 count) {
149     SET_SEG(ES, FLATPTR_TO_SEG(ptr_fl));
150     outsb(port, (u8*)FLATPTR_TO_OFFSET(ptr_fl), count);
151 }
152 static inline void outsw_fl(u16 port, void *ptr_fl, u16 count) {
153     SET_SEG(ES, FLATPTR_TO_SEG(ptr_fl));
154     outsw(port, (u16*)FLATPTR_TO_OFFSET(ptr_fl), count);
155 }
156 static inline void outsl_fl(u16 port, void *ptr_fl, u16 count) {
157     SET_SEG(ES, FLATPTR_TO_SEG(ptr_fl));
158     outsl(port, (u32*)FLATPTR_TO_OFFSET(ptr_fl), count);
159 }
160
161 extern void __force_link_error__only_in_32bit() __attribute__ ((noreturn));
162 #define ASSERT16() do { } while (0)
163 #define ASSERT32() __force_link_error__only_in_32bit()
164
165 #else
166
167 // In 32-bit mode there is no need to mess with the segments.
168 #define GET_FARVAR(seg, var) \
169     (*((typeof(&(var)))MAKE_FLATPTR((seg), &(var))))
170 #define SET_FARVAR(seg, var, val) \
171     do { GET_FARVAR((seg), (var)) = (val); } while (0)
172 #define GET_VAR(seg, var) (var)
173 #define SET_VAR(seg, var, val) do { (var) = (val); } while (0)
174 #define SET_SEG(SEG, value) ((void)(value))
175 #define GET_SEG(SEG) 0
176 #define GET_FLATPTR(ptr) (ptr)
177 #define SET_FLATPTR(ptr, val) do { (ptr) = (val); } while (0)
178
179 #define insb_fl(port, ptr_fl, count) insb(port, ptr_fl, count)
180 #define insw_fl(port, ptr_fl, count) insw(port, ptr_fl, count)
181 #define insl_fl(port, ptr_fl, count) insl(port, ptr_fl, count)
182 #define outsb_fl(port, ptr_fl, count) outsb(port, ptr_fl, count)
183 #define outsw_fl(port, ptr_fl, count) outsw(port, ptr_fl, count)
184 #define outsl_fl(port, ptr_fl, count) outsl(port, ptr_fl, count)
185
186 extern void __force_link_error__only_in_16bit() __attribute__ ((noreturn));
187 #define ASSERT16() __force_link_error__only_in_16bit()
188 #define ASSERT32() do { } while (0)
189
190 #endif
191
192 #endif // farptr.h