1 // Code to access multiple segments within gcc.
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU GPLv3 license.
9 #include "ioport.h" // insb
11 // Low level macros for reading/writing memory via a segment selector.
12 #define READ8_SEG(SEG, var) ({ \
14 __asm__ __volatile__("movb %%" #SEG ":%1, %b0" \
15 : "=Qi"(__value) : "m"(var)); \
17 #define READ16_SEG(SEG, var) ({ \
19 __asm__ __volatile__("movw %%" #SEG ":%1, %w0" \
20 : "=ri"(__value) : "m"(var)); \
22 #define READ32_SEG(SEG, var) ({ \
24 __asm__ __volatile__("movl %%" #SEG ":%1, %0" \
25 : "=ri"(__value) : "m"(var)); \
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))
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) ({ \
42 __asm__ __volatile__("movw %%" #SEG ", %w0" : "=r"(__seg)); \
45 // Macros for automatically choosing the appropriate memory size
47 extern void __force_link_error__unknown_type();
49 #define __GET_VAR(seg, var) ({ \
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); \
58 __force_link_error__unknown_type(); \
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)); \
69 __force_link_error__unknown_type(); \
72 // Macros for accessing a variable in another segment. (They
73 // automatically update the %es segment and then make the appropriate
75 #define __GET_FARVAR(seg, var) ({ \
77 GET_VAR(ES, (var)); })
78 #define __SET_FARVAR(seg, var, val) do { \
79 typeof(var) __sfv_val = (val); \
81 SET_VAR(ES, (var), __sfv_val); \
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) \
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)))
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))
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);
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);
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);
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);
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);
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);
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)
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)