Fix asm dependencies on segment based writes.
[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 WRITE8_SEG(SEG, var, value)                             \
32     __asm__("movb %b1, %%" #SEG ":%0" : "=m"(var)               \
33             : "Q"(value), "m"(__segment_ ## SEG))
34 #define WRITE16_SEG(SEG, var, value)                            \
35     __asm__("movw %w1, %%" #SEG ":%0" : "=m"(var)               \
36             : "r"(value), "m"(__segment_ ## SEG))
37 #define WRITE32_SEG(SEG, var, value)                            \
38     __asm__("movl %1, %%" #SEG ":%0" : "=m"(var)                \
39             : "r"(value), "m"(__segment_ ## SEG))
40
41 // Low level macros for getting/setting a segment register.
42 #define __SET_SEG(SEG, value)                                   \
43     __asm__("movw %w1, %%" #SEG : "=m"(__segment_ ## SEG)       \
44             : "r"(value))
45 #define __GET_SEG(SEG) ({                                       \
46     u16 __seg;                                                  \
47     __asm__("movw %%" #SEG ", %w0" : "=r"(__seg)                \
48             : "m"(__segment_ ## SEG));                          \
49     __seg;})
50
51 // Macros for automatically choosing the appropriate memory size
52 // access method.
53 extern void __force_link_error__unknown_type();
54
55 #define __GET_VAR(seg, var) ({                                          \
56     typeof(var) __val;                                                  \
57     if (__builtin_types_compatible_p(typeof(__val), u8)                 \
58         || __builtin_types_compatible_p(typeof(__val), s8))             \
59         __val = READ8_SEG(seg, var);                                    \
60     else if (__builtin_types_compatible_p(typeof(__val), u16)           \
61              || __builtin_types_compatible_p(typeof(__val), s16))       \
62         __val = READ16_SEG(seg, var);                                   \
63     else if (__builtin_types_compatible_p(typeof(__val), u32)           \
64              || __builtin_types_compatible_p(typeof(__val), s32))       \
65         __val = READ32_SEG(seg, var);                                   \
66     else                                                                \
67         __force_link_error__unknown_type();                             \
68     __val; })
69
70 #define __SET_VAR(seg, var, val) do {                                   \
71         if (__builtin_types_compatible_p(typeof(var), u8)               \
72             || __builtin_types_compatible_p(typeof(var), s8))           \
73             WRITE8_SEG(seg, var, (val));                                \
74         else if (__builtin_types_compatible_p(typeof(var), u16)         \
75                  || __builtin_types_compatible_p(typeof(var), s16))     \
76             WRITE16_SEG(seg, var, (val));                               \
77         else if (__builtin_types_compatible_p(typeof(var), u32)         \
78                  || __builtin_types_compatible_p(typeof(var), s32))     \
79             WRITE32_SEG(seg, var, (val));                               \
80         else                                                            \
81             __force_link_error__unknown_type();                         \
82     } while (0)
83
84 // Macros for accessing a variable in another segment.  (They
85 // automatically update the %es segment and then make the appropriate
86 // access.)
87 #define __GET_FARVAR(seg, var) ({               \
88     SET_SEG(ES, (seg));                         \
89     GET_VAR(ES, (var)); })
90 #define __SET_FARVAR(seg, var, val) do {        \
91         typeof(var) __sfv_val = (val);          \
92         SET_SEG(ES, (seg));                     \
93         SET_VAR(ES, (var), __sfv_val);          \
94     } while (0)
95
96 // Macros for accesssing a 32bit pointer from 16bit mode.  (They
97 // automatically update the %es segment, break the pointer into
98 // segment/offset, and then make the access.)
99 #define __GET_FARPTR(ptr) ({                                            \
100     typeof(&(ptr)) __ptr = &(ptr);                                      \
101     GET_FARVAR(FARPTR_TO_SEG(__ptr)                                     \
102                , *(typeof(__ptr))FARPTR_TO_OFFSET(__ptr)); })
103 #define __SET_FARPTR(ptr, val) do {                                     \
104         typeof (&(ptr)) __ptr = &(ptr);                                 \
105         SET_FARVAR(FARPTR_TO_SEG(__ptr)                                 \
106                    , *(typeof(__ptr))FARPTR_TO_OFFSET(__ptr)            \
107                    , (val));                                            \
108     } while (0)
109
110 // Macros for converting to/from 32bit style pointers to their
111 // equivalent 16bit segment/offset values.
112 #define FARPTR_TO_SEG(p) (((u32)(p)) >> 4)
113 #define FARPTR_TO_OFFSET(p) (((u32)(p)) & 0xf)
114 #define MAKE_FARPTR(seg,off) ((void*)(((seg)<<4)+(off)))
115
116
117 #ifdef MODE16
118
119 // Definitions when in 16 bit mode.
120 #define GET_FARVAR(seg, var) __GET_FARVAR((seg), (var))
121 #define SET_FARVAR(seg, var, val) __SET_FARVAR((seg), (var), (val))
122 #define GET_VAR(seg, var) __GET_VAR(seg, (var))
123 #define SET_VAR(seg, var, val) __SET_VAR(seg, (var), (val))
124 #define SET_SEG(SEG, value) __SET_SEG(SEG, (value))
125 #define GET_SEG(SEG) __GET_SEG(SEG)
126 #define GET_FARPTR(ptr) __GET_FARPTR(ptr)
127 #define SET_FARPTR(ptr, val) __SET_FARPTR((ptr), (val))
128
129 static inline void insb_far(u16 port, void *farptr, u16 count) {
130     SET_SEG(ES, FARPTR_TO_SEG(farptr));
131     insb(port, (u8*)FARPTR_TO_OFFSET(farptr), count);
132 }
133 static inline void insw_far(u16 port, void *farptr, u16 count) {
134     SET_SEG(ES, FARPTR_TO_SEG(farptr));
135     insw(port, (u16*)FARPTR_TO_OFFSET(farptr), count);
136 }
137 static inline void insl_far(u16 port, void *farptr, u16 count) {
138     SET_SEG(ES, FARPTR_TO_SEG(farptr));
139     insl(port, (u32*)FARPTR_TO_OFFSET(farptr), count);
140 }
141 static inline void outsb_far(u16 port, void *farptr, u16 count) {
142     SET_SEG(ES, FARPTR_TO_SEG(farptr));
143     outsb(port, (u8*)FARPTR_TO_OFFSET(farptr), count);
144 }
145 static inline void outsw_far(u16 port, void *farptr, u16 count) {
146     SET_SEG(ES, FARPTR_TO_SEG(farptr));
147     outsw(port, (u16*)FARPTR_TO_OFFSET(farptr), count);
148 }
149 static inline void outsl_far(u16 port, void *farptr, u16 count) {
150     SET_SEG(ES, FARPTR_TO_SEG(farptr));
151     outsl(port, (u32*)FARPTR_TO_OFFSET(farptr), count);
152 }
153
154 #else
155
156 // In 32-bit mode there is no need to mess with the segments.
157 #define GET_FARVAR(seg, var) \
158     (*((typeof(&(var)))MAKE_FARPTR((seg), (u32)&(var))))
159 #define SET_FARVAR(seg, var, val) \
160     do { GET_FARVAR((seg), (var)) = (val); } while (0)
161 #define GET_VAR(seg, var) (var)
162 #define SET_VAR(seg, var, val) do { (var) = (val); } while (0)
163 #define SET_SEG(SEG, value) ((void)(value))
164 #define GET_SEG(SEG) 0
165 #define GET_FARPTR(ptr) (ptr)
166 #define SET_FARPTR(ptr, val) do { (ptr) = (val); } while (0)
167
168 #define insb_far(port, farptr, count) insb(port, farptr, count)
169 #define insw_far(port, farptr, count) insw(port, farptr, count)
170 #define insl_far(port, farptr, count) insl(port, farptr, count)
171 #define outsb_far(port, farptr, count) outsb(port, farptr, count)
172 #define outsw_far(port, farptr, count) outsw(port, farptr, count)
173 #define outsl_far(port, farptr, count) outsl(port, farptr, count)
174
175 #endif
176
177 #endif // farptr.h