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(PTR_TO_SEG(__ptr), *(typeof __ptr)PTR_TO_OFFSET(__ptr)); })
90 #define __SET_FARPTR(ptr, val) do { \
91 typeof (&(ptr)) __ptr = (ptr); \
92 SET_FARVAR(PTR_TO_SEG(__ptr), *(typeof __ptr)PTR_TO_OFFSET(__ptr) \
96 // Macros for converting to/from 32bit style pointers to their
97 // equivalent 16bit segment/offset values.
98 #define FARPTR_TO_SEG(p) (((u32)(p)) >> 4)
99 #define FARPTR_TO_OFFSET(p) (((u32)(p)) & 0xf)
100 #define MAKE_FARPTR(seg,off) ((void*)(((seg)<<4)+(off)))
105 // Definitions when in 16 bit mode.
106 #define GET_FARVAR(seg, var) __GET_FARVAR((seg), (var))
107 #define SET_FARVAR(seg, var, val) __SET_FARVAR((seg), (var), (val))
108 #define GET_VAR(seg, var) __GET_VAR(seg, (var))
109 #define SET_VAR(seg, var, val) __SET_VAR(seg, (var), (val))
110 #define SET_SEG(SEG, value) __SET_SEG(SEG, (value))
111 #define GET_SEG(SEG) __GET_SEG(SEG)
112 #define GET_FARPTR(ptr) __GET_FARPTR(ptr)
113 #define SET_FARPTR(ptr, val) __SET_FARPTR((ptr), (val))
115 static inline void insb_far(u16 port, void *farptr, u16 count) {
116 SET_SEG(ES, FARPTR_TO_SEG(farptr));
117 insb(port, (u8*)FARPTR_TO_OFFSET(farptr), count);
119 static inline void insw_far(u16 port, void *farptr, u16 count) {
120 SET_SEG(ES, FARPTR_TO_SEG(farptr));
121 insw(port, (u16*)FARPTR_TO_OFFSET(farptr), count);
123 static inline void insl_far(u16 port, void *farptr, u16 count) {
124 SET_SEG(ES, FARPTR_TO_SEG(farptr));
125 insl(port, (u32*)FARPTR_TO_OFFSET(farptr), count);
127 static inline void outsb_far(u16 port, void *farptr, u16 count) {
128 SET_SEG(ES, FARPTR_TO_SEG(farptr));
129 outsb(port, (u8*)FARPTR_TO_OFFSET(farptr), count);
131 static inline void outsw_far(u16 port, void *farptr, u16 count) {
132 SET_SEG(ES, FARPTR_TO_SEG(farptr));
133 outsw(port, (u16*)FARPTR_TO_OFFSET(farptr), count);
135 static inline void outsl_far(u16 port, void *farptr, u16 count) {
136 SET_SEG(ES, FARPTR_TO_SEG(farptr));
137 outsl(port, (u32*)FARPTR_TO_OFFSET(farptr), count);
142 // In 32-bit mode there is no need to mess with the segments.
143 #define GET_FARVAR(seg, var) \
144 (*((typeof(&(var)))MAKE_FARPTR((seg), (u32)&(var))))
145 #define SET_FARVAR(seg, var, val) \
146 do { GET_FARVAR((seg), (var)) = (val); } while (0)
147 #define GET_VAR(seg, var) (var)
148 #define SET_VAR(seg, var, val) do { (var) = (val); } while (0)
149 #define SET_SEG(SEG, value) ((void)(value))
150 #define GET_SEG(SEG) 0
151 #define GET_FARPTR(ptr) (ptr)
152 #define SET_FARPTR(ptr, val) do { (var) = (val); } while (0)
154 #define insb_far(port, farptr, count) insb(port, farptr, count)
155 #define insw_far(port, farptr, count) insw(port, farptr, count)
156 #define insl_far(port, farptr, count) insl(port, farptr, count)
157 #define outsb_far(port, farptr, count) outsb(port, farptr, count)
158 #define outsw_far(port, farptr, count) outsw(port, farptr, count)
159 #define outsl_far(port, farptr, count) outsl(port, farptr, count)