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 LGPLv3 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;
14 extern u16 __segment_FS, __segment_GS;
16 // Low level macros for reading/writing memory via a segment selector.
17 #define READ8_SEG(SEG, value, var) \
18 __asm__("movb %%" #SEG ":%1, %b0" : "=Qi"(value) \
19 : "m"(var), "m"(__segment_ ## SEG))
20 #define READ16_SEG(SEG, value, var) \
21 __asm__("movw %%" #SEG ":%1, %w0" : "=ri"(value) \
22 : "m"(var), "m"(__segment_ ## SEG))
23 #define READ32_SEG(SEG, value, var) \
24 __asm__("movl %%" #SEG ":%1, %0" : "=ri"(value) \
25 : "m"(var), "m"(__segment_ ## SEG))
26 #define READ64_SEG(SEG, value, var) do { \
27 union u64_u32_u *__w64_ptr = (union u64_u32_u *)&(value); \
28 union u64_u32_u *__r64_ptr = (union u64_u32_u *)&(var); \
29 READ32_SEG(SEG, __w64_ptr->hi, __r64_ptr->hi); \
30 READ32_SEG(SEG, __w64_ptr->lo, __r64_ptr->lo); \
32 #define WRITE8_SEG(SEG, var, value) \
33 __asm__("movb %b1, %%" #SEG ":%0" : "=m"(var) \
34 : "Q"(value), "m"(__segment_ ## SEG))
35 #define WRITE16_SEG(SEG, var, value) \
36 __asm__("movw %w1, %%" #SEG ":%0" : "=m"(var) \
37 : "r"(value), "m"(__segment_ ## SEG))
38 #define WRITE32_SEG(SEG, var, value) \
39 __asm__("movl %1, %%" #SEG ":%0" : "=m"(var) \
40 : "r"(value), "m"(__segment_ ## SEG))
41 #define WRITE64_SEG(SEG, var, value) do { \
42 union u64_u32_u __value; \
43 union u64_u32_u *__w64_ptr = (union u64_u32_u *)&(var); \
44 __value.val = (value); \
45 WRITE32_SEG(SEG, __w64_ptr->hi, __value.hi); \
46 WRITE32_SEG(SEG, __w64_ptr->lo, __value.lo); \
49 // Low level macros for getting/setting a segment register.
50 #define __SET_SEG(SEG, value) \
51 __asm__("movw %w1, %%" #SEG : "=m"(__segment_ ## SEG) \
53 #define __GET_SEG(SEG) ({ \
55 __asm__("movw %%" #SEG ", %w0" : "=r"(__seg) \
56 : "m"(__segment_ ## SEG)); \
59 // Macros for automatically choosing the appropriate memory size
61 extern void __force_link_error__unknown_type();
63 #define __GET_VAR(seg, var) ({ \
65 if (sizeof(__val) == 1) \
66 READ8_SEG(seg, __val, var); \
67 else if (sizeof(__val) == 2) \
68 READ16_SEG(seg, __val, var); \
69 else if (sizeof(__val) == 4) \
70 READ32_SEG(seg, __val, var); \
71 else if (sizeof(__val) == 8) \
72 READ64_SEG(seg, __val, var); \
74 __force_link_error__unknown_type(); \
77 #define __SET_VAR(seg, var, val) do { \
78 if (sizeof(var) == 1) \
79 WRITE8_SEG(seg, var, (val)); \
80 else if (sizeof(var) == 2) \
81 WRITE16_SEG(seg, var, (val)); \
82 else if (sizeof(var) == 4) \
83 WRITE32_SEG(seg, var, (val)); \
84 else if (sizeof(var) == 8) \
85 WRITE64_SEG(seg, var, (val)); \
87 __force_link_error__unknown_type(); \
90 // Macros for accessing a variable in another segment. (They
91 // automatically update the %es segment and then make the appropriate
93 #define __GET_FARVAR(seg, var) ({ \
95 GET_VAR(ES, (var)); })
96 #define __SET_FARVAR(seg, var, val) do { \
97 typeof(var) __sfv_val = (val); \
99 SET_VAR(ES, (var), __sfv_val); \
102 // Macros for accesssing a 32bit pointer from 16bit real mode. (They
103 // automatically update the %es segment, break the pointer into
104 // segment/offset, and then make the access.)
105 #define __GET_FARPTR(ptr) ({ \
106 typeof(&(ptr)) __ptr = &(ptr); \
107 GET_FARVAR(FARPTR_TO_SEG(__ptr) \
108 , *(typeof(__ptr))FARPTR_TO_OFFSET(__ptr)); })
109 #define __SET_FARPTR(ptr, val) do { \
110 typeof (&(ptr)) __ptr = &(ptr); \
111 SET_FARVAR(FARPTR_TO_SEG(__ptr) \
112 , *(typeof(__ptr))FARPTR_TO_OFFSET(__ptr) \
116 // Macros for converting to/from 32bit style pointers to their
117 // equivalent 16bit segment/offset values.
118 #define FARPTR_TO_SEG(p) (((u32)(p)) >> 4)
119 #define FARPTR_TO_OFFSET(p) (((u32)(p)) & 0xf)
120 #define MAKE_FARPTR(seg,off) ((void*)(((u32)(seg)<<4)+(u32)(off)))
125 // Definitions when in 16 bit mode.
126 #define GET_FARVAR(seg, var) __GET_FARVAR((seg), (var))
127 #define SET_FARVAR(seg, var, val) __SET_FARVAR((seg), (var), (val))
128 #define GET_VAR(seg, var) __GET_VAR(seg, (var))
129 #define SET_VAR(seg, var, val) __SET_VAR(seg, (var), (val))
130 #define SET_SEG(SEG, value) __SET_SEG(SEG, (value))
131 #define GET_SEG(SEG) __GET_SEG(SEG)
132 #define GET_FARPTR(ptr) __GET_FARPTR(ptr)
133 #define SET_FARPTR(ptr, val) __SET_FARPTR((ptr), (val))
135 static inline void insb_far(u16 port, void *farptr, u16 count) {
136 SET_SEG(ES, FARPTR_TO_SEG(farptr));
137 insb(port, (u8*)FARPTR_TO_OFFSET(farptr), count);
139 static inline void insw_far(u16 port, void *farptr, u16 count) {
140 SET_SEG(ES, FARPTR_TO_SEG(farptr));
141 insw(port, (u16*)FARPTR_TO_OFFSET(farptr), count);
143 static inline void insl_far(u16 port, void *farptr, u16 count) {
144 SET_SEG(ES, FARPTR_TO_SEG(farptr));
145 insl(port, (u32*)FARPTR_TO_OFFSET(farptr), count);
147 static inline void outsb_far(u16 port, void *farptr, u16 count) {
148 SET_SEG(ES, FARPTR_TO_SEG(farptr));
149 outsb(port, (u8*)FARPTR_TO_OFFSET(farptr), count);
151 static inline void outsw_far(u16 port, void *farptr, u16 count) {
152 SET_SEG(ES, FARPTR_TO_SEG(farptr));
153 outsw(port, (u16*)FARPTR_TO_OFFSET(farptr), count);
155 static inline void outsl_far(u16 port, void *farptr, u16 count) {
156 SET_SEG(ES, FARPTR_TO_SEG(farptr));
157 outsl(port, (u32*)FARPTR_TO_OFFSET(farptr), count);
162 // In 32-bit mode there is no need to mess with the segments.
163 #define GET_FARVAR(seg, var) \
164 (*((typeof(&(var)))MAKE_FARPTR((seg), (u32)&(var))))
165 #define SET_FARVAR(seg, var, val) \
166 do { GET_FARVAR((seg), (var)) = (val); } while (0)
167 #define GET_VAR(seg, var) (var)
168 #define SET_VAR(seg, var, val) do { (var) = (val); } while (0)
169 #define SET_SEG(SEG, value) ((void)(value))
170 #define GET_SEG(SEG) 0
171 #define GET_FARPTR(ptr) (ptr)
172 #define SET_FARPTR(ptr, val) do { (ptr) = (val); } while (0)
174 #define insb_far(port, farptr, count) insb(port, farptr, count)
175 #define insw_far(port, farptr, count) insw(port, farptr, count)
176 #define insl_far(port, farptr, count) insl(port, farptr, count)
177 #define outsb_far(port, farptr, count) outsb(port, farptr, count)
178 #define outsw_far(port, farptr, count) outsw(port, farptr, count)
179 #define outsl_far(port, farptr, count) outsl(port, farptr, count)