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 // 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;
15 // Low level macros for reading/writing memory via a segment selector.
16 #define READ8_SEG(SEG, value, var) \
17 __asm__("movb %%" #SEG ":%1, %b0" : "=Qi"(value) \
18 : "m"(var), "m"(__segment_ ## SEG))
19 #define READ16_SEG(SEG, value, var) \
20 __asm__("movw %%" #SEG ":%1, %w0" : "=ri"(value) \
21 : "m"(var), "m"(__segment_ ## SEG))
22 #define READ32_SEG(SEG, value, var) \
23 __asm__("movl %%" #SEG ":%1, %0" : "=ri"(value) \
24 : "m"(var), "m"(__segment_ ## SEG))
25 #define READ64_SEG(SEG, value, var) do { \
26 union u64_u32_u *__w64_ptr = (union u64_u32_u *)&(value); \
27 union u64_u32_u *__r64_ptr = (union u64_u32_u *)&(var); \
28 READ32_SEG(SEG, __w64_ptr->hi, __r64_ptr->hi); \
29 READ32_SEG(SEG, __w64_ptr->lo, __r64_ptr->lo); \
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 #define WRITE64_SEG(SEG, var, value) do { \
41 union u64_u32_u __value; \
42 union u64_u32_u *__w64_ptr = (union u64_u32_u *)&(var); \
43 __value.val = (value); \
44 WRITE32_SEG(SEG, __w64_ptr->hi, __value.hi); \
45 WRITE32_SEG(SEG, __w64_ptr->lo, __value.lo); \
48 // Low level macros for getting/setting a segment register.
49 #define __SET_SEG(SEG, value) \
50 __asm__("movw %w1, %%" #SEG : "=m"(__segment_ ## SEG) \
52 #define __GET_SEG(SEG) ({ \
54 __asm__("movw %%" #SEG ", %w0" : "=r"(__seg) \
55 : "m"(__segment_ ## SEG)); \
58 // Macros for automatically choosing the appropriate memory size
60 extern void __force_link_error__unknown_type();
62 #define __GET_VAR(seg, var) ({ \
64 if (sizeof(__val) == 1) \
65 READ8_SEG(seg, __val, var); \
66 else if (sizeof(__val) == 2) \
67 READ16_SEG(seg, __val, var); \
68 else if (sizeof(__val) == 4) \
69 READ32_SEG(seg, __val, var); \
70 else if (sizeof(__val) == 8) \
71 READ64_SEG(seg, __val, var); \
73 __force_link_error__unknown_type(); \
76 #define __SET_VAR(seg, var, val) do { \
77 if (sizeof(var) == 1) \
78 WRITE8_SEG(seg, var, (val)); \
79 else if (sizeof(var) == 2) \
80 WRITE16_SEG(seg, var, (val)); \
81 else if (sizeof(var) == 4) \
82 WRITE32_SEG(seg, var, (val)); \
83 else if (sizeof(var) == 8) \
84 WRITE64_SEG(seg, var, (val)); \
86 __force_link_error__unknown_type(); \
89 // Macros for accessing a variable in another segment. (They
90 // automatically update the %es segment and then make the appropriate
92 #define __GET_FARVAR(seg, var) ({ \
94 GET_VAR(ES, (var)); })
95 #define __SET_FARVAR(seg, var, val) do { \
96 typeof(var) __sfv_val = (val); \
98 SET_VAR(ES, (var), __sfv_val); \
101 // Macros for accesssing a 32bit pointer from 16bit mode. (They
102 // automatically update the %es segment, break the pointer into
103 // segment/offset, and then make the access.)
104 #define __GET_FARPTR(ptr) ({ \
105 typeof(&(ptr)) __ptr = &(ptr); \
106 GET_FARVAR(FARPTR_TO_SEG(__ptr) \
107 , *(typeof(__ptr))FARPTR_TO_OFFSET(__ptr)); })
108 #define __SET_FARPTR(ptr, val) do { \
109 typeof (&(ptr)) __ptr = &(ptr); \
110 SET_FARVAR(FARPTR_TO_SEG(__ptr) \
111 , *(typeof(__ptr))FARPTR_TO_OFFSET(__ptr) \
115 // Macros for converting to/from 32bit style pointers to their
116 // equivalent 16bit segment/offset values.
117 #define FARPTR_TO_SEG(p) (((u32)(p)) >> 4)
118 #define FARPTR_TO_OFFSET(p) (((u32)(p)) & 0xf)
119 #define MAKE_FARPTR(seg,off) ((void*)(((seg)<<4)+(off)))
124 // Definitions when in 16 bit mode.
125 #define GET_FARVAR(seg, var) __GET_FARVAR((seg), (var))
126 #define SET_FARVAR(seg, var, val) __SET_FARVAR((seg), (var), (val))
127 #define GET_VAR(seg, var) __GET_VAR(seg, (var))
128 #define SET_VAR(seg, var, val) __SET_VAR(seg, (var), (val))
129 #define SET_SEG(SEG, value) __SET_SEG(SEG, (value))
130 #define GET_SEG(SEG) __GET_SEG(SEG)
131 #define GET_FARPTR(ptr) __GET_FARPTR(ptr)
132 #define SET_FARPTR(ptr, val) __SET_FARPTR((ptr), (val))
134 static inline void insb_far(u16 port, void *farptr, u16 count) {
135 SET_SEG(ES, FARPTR_TO_SEG(farptr));
136 insb(port, (u8*)FARPTR_TO_OFFSET(farptr), count);
138 static inline void insw_far(u16 port, void *farptr, u16 count) {
139 SET_SEG(ES, FARPTR_TO_SEG(farptr));
140 insw(port, (u16*)FARPTR_TO_OFFSET(farptr), count);
142 static inline void insl_far(u16 port, void *farptr, u16 count) {
143 SET_SEG(ES, FARPTR_TO_SEG(farptr));
144 insl(port, (u32*)FARPTR_TO_OFFSET(farptr), count);
146 static inline void outsb_far(u16 port, void *farptr, u16 count) {
147 SET_SEG(ES, FARPTR_TO_SEG(farptr));
148 outsb(port, (u8*)FARPTR_TO_OFFSET(farptr), count);
150 static inline void outsw_far(u16 port, void *farptr, u16 count) {
151 SET_SEG(ES, FARPTR_TO_SEG(farptr));
152 outsw(port, (u16*)FARPTR_TO_OFFSET(farptr), count);
154 static inline void outsl_far(u16 port, void *farptr, u16 count) {
155 SET_SEG(ES, FARPTR_TO_SEG(farptr));
156 outsl(port, (u32*)FARPTR_TO_OFFSET(farptr), count);
161 // In 32-bit mode there is no need to mess with the segments.
162 #define GET_FARVAR(seg, var) \
163 (*((typeof(&(var)))MAKE_FARPTR((seg), (u32)&(var))))
164 #define SET_FARVAR(seg, var, val) \
165 do { GET_FARVAR((seg), (var)) = (val); } while (0)
166 #define GET_VAR(seg, var) (var)
167 #define SET_VAR(seg, var, val) do { (var) = (val); } while (0)
168 #define SET_SEG(SEG, value) ((void)(value))
169 #define GET_SEG(SEG) 0
170 #define GET_FARPTR(ptr) (ptr)
171 #define SET_FARPTR(ptr, val) do { (ptr) = (val); } while (0)
173 #define insb_far(port, farptr, count) insb(port, farptr, count)
174 #define insw_far(port, farptr, count) insw(port, farptr, count)
175 #define insl_far(port, farptr, count) insl(port, farptr, count)
176 #define outsb_far(port, farptr, count) outsb(port, farptr, count)
177 #define outsw_far(port, farptr, count) outsw(port, farptr, count)
178 #define outsl_far(port, farptr, count) outsl(port, farptr, count)