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