Fix bug in SET_FARVAR.
[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 #define READ8_SEG(SEG, var) ({                                          \
12     u8 __value;                                                         \
13     __asm__ __volatile__("movb %%" #SEG ":%1, %b0"                      \
14                          : "=Qi"(__value) : "m"(var));                  \
15     __value; })
16 #define READ16_SEG(SEG, var) ({                                         \
17     u16 __value;                                                        \
18     __asm__ __volatile__("movw %%" #SEG ":%1, %w0"                      \
19                          : "=ri"(__value) : "m"(var));                  \
20     __value; })
21 #define READ32_SEG(SEG, var) ({                                         \
22     u32 __value;                                                        \
23     __asm__ __volatile__("movl %%" #SEG ":%1, %0"                       \
24                          : "=ri"(__value) : "m"(var));                  \
25     __value; })
26 #define WRITE8_SEG(SEG, var, value)                     \
27     __asm__ __volatile__("movb %b0, %%" #SEG ":%1"      \
28                          : : "Q"(value), "m"(var))
29 #define WRITE16_SEG(SEG, var, value)                    \
30     __asm__ __volatile__("movw %w0, %%" #SEG ":%1"      \
31                          : : "r"(value), "m"(var))
32 #define WRITE32_SEG(SEG, var, value)                    \
33     __asm__ __volatile__("movl %0, %%" #SEG ":%1"       \
34                          : : "r"(value), "m"(var))
35
36 extern void __force_link_error__unknown_type();
37
38 #define __GET_VAR(seg, var) ({                                  \
39     typeof(var) __val;                                          \
40     if (__builtin_types_compatible_p(typeof(__val), u8))        \
41         __val = READ8_SEG(seg, var);                            \
42     else if (__builtin_types_compatible_p(typeof(__val), u16))  \
43         __val = READ16_SEG(seg, var);                           \
44     else if (__builtin_types_compatible_p(typeof(__val), u32))  \
45         __val = READ32_SEG(seg, var);                           \
46     else                                                        \
47         __force_link_error__unknown_type();                     \
48     __val; })
49
50 #define __SET_VAR(seg, var, val) do {                             \
51         if (__builtin_types_compatible_p(typeof(var), u8))        \
52             WRITE8_SEG(seg, var, (val));                          \
53         else if (__builtin_types_compatible_p(typeof(var), u16))  \
54             WRITE16_SEG(seg, var, (val));                         \
55         else if (__builtin_types_compatible_p(typeof(var), u32))  \
56             WRITE32_SEG(seg, var, (val));                         \
57         else                                                      \
58             __force_link_error__unknown_type();                   \
59     } while (0)
60
61 #define __SET_SEG(SEG, value)                                   \
62     __asm__ __volatile__("movw %w0, %%" #SEG : : "r"(value))
63 #define __GET_SEG(SEG) ({                                       \
64     u16 __seg;                                                  \
65     __asm__ __volatile__("movw %%" #SEG ", %w0" : "=r"(__seg)); \
66     __seg;})
67
68 #define GET_FARVAR(seg, var) ({                 \
69     SET_SEG(ES, (seg));                         \
70     GET_VAR(ES, (var)); })
71 #define SET_FARVAR(seg, var, val) do {          \
72         typeof(var) __sfv_val = (val);          \
73         SET_SEG(ES, (seg));                     \
74         SET_VAR(ES, (var), __sfv_val);          \
75     } while (0)
76
77 #define PTR_TO_SEG(p) ((((u32)(p)) >> 4) & 0xf000)
78 #define PTR_TO_OFFSET(p) (((u32)(p)) & 0xffff)
79
80 #define __GET_FARPTR(ptr) ({                                            \
81     typeof (&(ptr)) __ptr;                                              \
82     GET_FARVAR(PTR_TO_SEG(__ptr), *(typeof __ptr)PTR_TO_OFFSET(__ptr)); })
83 #define __SET_FARVAR(ptr, val) do {                                     \
84         typeof (&(ptr)) __ptr;                                          \
85         SET_FARVAR(PTR_TO_SEG(__ptr), *(typeof __ptr)PTR_TO_OFFSET(__ptr) \
86                    , (val));                                            \
87     } while (0)
88
89 #ifdef MODE16
90 #define GET_VAR(seg, var) __GET_VAR(seg, (var))
91 #define SET_VAR(seg, var, val) __SET_VAR(seg, (var), (val))
92 #define SET_SEG(SEG, value) __SET_SEG(SEG, (value))
93 #define GET_SEG(SEG) __GET_SEG(SEG)
94 #define GET_FARPTR(ptr) __GET_FARPTR(ptr)
95 #define SET_FARPTR(ptr, val) __SET_FARPTR((ptr), (val))
96 #else
97 // In 32-bit mode there is no need to mess with the segments.
98 #define GET_VAR(seg, var) (var)
99 #define SET_VAR(seg, var, val) do { (var) = (val); } while (0)
100 #define SET_SEG(SEG, value) ((void)(value))
101 #define GET_SEG(SEG) 0
102 #define GET_FARPTR(ptr) (ptr)
103 #define SET_FARPTR(ptr, val) do { (var) = (val); } while (0)
104 #endif
105
106 static inline void insb_seg(u16 port, u16 segment, u16 offset, u16 count) {
107     SET_SEG(ES, segment);
108     insb(port, (u8*)(offset+0), count);
109 }
110 static inline void insw_seg(u16 port, u16 segment, u16 offset, u16 count) {
111     SET_SEG(ES, segment);
112     insw(port, (u16*)(offset+0), count);
113 }
114 static inline void insl_seg(u16 port, u16 segment, u16 offset, u16 count) {
115     SET_SEG(ES, segment);
116     insl(port, (u32*)(offset+0), count);
117 }
118 static inline void outsb_seg(u16 port, u16 segment, u16 offset, u16 count) {
119     SET_SEG(ES, segment);
120     outsb(port, (u8*)(offset+0), count);
121 }
122 static inline void outsw_seg(u16 port, u16 segment, u16 offset, u16 count) {
123     SET_SEG(ES, segment);
124     outsw(port, (u16*)(offset+0), count);
125 }
126 static inline void outsl_seg(u16 port, u16 segment, u16 offset, u16 count) {
127     SET_SEG(ES, segment);
128     outsl(port, (u32*)(offset+0), count);
129 }
130
131 #endif // farptr.h