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 #define READ8_SEG(SEG, var) ({ \
13 __asm__ __volatile__("movb %%" #SEG ":%1, %b0" \
14 : "=Qi"(__value) : "m"(var)); \
16 #define READ16_SEG(SEG, var) ({ \
18 __asm__ __volatile__("movw %%" #SEG ":%1, %w0" \
19 : "=ri"(__value) : "m"(var)); \
21 #define READ32_SEG(SEG, var) ({ \
23 __asm__ __volatile__("movl %%" #SEG ":%1, %0" \
24 : "=ri"(__value) : "m"(var)); \
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))
36 extern void __force_link_error__unknown_type();
38 #define __GET_VAR(seg, var) ({ \
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); \
47 __force_link_error__unknown_type(); \
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)); \
58 __force_link_error__unknown_type(); \
61 #define __SET_SEG(SEG, value) \
62 __asm__ __volatile__("movw %w0, %%" #SEG : : "r"(value))
63 #define __GET_SEG(SEG) ({ \
65 __asm__ __volatile__("movw %%" #SEG ", %w0" : "=r"(__seg)); \
68 #define GET_FARVAR(seg, var) ({ \
70 GET_VAR(ES, (var)); })
71 #define SET_FARVAR(seg, var, val) do { \
73 SET_VAR(ES, (var), val); \
76 #define PTR_TO_SEG(p) ((((u32)(p)) >> 4) & 0xf000)
77 #define PTR_TO_OFFSET(p) (((u32)(p)) & 0xffff)
79 #define __GET_FARPTR(ptr) ({ \
80 typeof (&(ptr)) __ptr; \
81 GET_FARVAR(PTR_TO_SEG(__ptr), *(typeof __ptr)PTR_TO_OFFSET(__ptr)); })
82 #define __SET_FARVAR(ptr, val) do { \
83 typeof (&(ptr)) __ptr; \
84 SET_FARVAR(PTR_TO_SEG(__ptr), *(typeof __ptr)PTR_TO_OFFSET(__ptr) \
89 #define GET_VAR(seg, var) __GET_VAR(seg, (var))
90 #define SET_VAR(seg, var, val) __SET_VAR(seg, (var), (val))
91 #define SET_SEG(SEG, value) __SET_SEG(SEG, (value))
92 #define GET_SEG(SEG) __GET_SEG(SEG)
93 #define GET_FARPTR(ptr) __GET_FARPTR(ptr)
94 #define SET_FARPTR(ptr, val) __SET_FARPTR((ptr), (val))
96 // In 32-bit mode there is no need to mess with the segments.
97 #define GET_VAR(seg, var) (var)
98 #define SET_VAR(seg, var, val) do { (var) = (val); } while (0)
99 #define SET_SEG(SEG, value) ((void)(value))
100 #define GET_SEG(SEG) 0
101 #define GET_FARPTR(ptr) (ptr)
102 #define SET_FARPTR(ptr, val) do { (var) = (val); } while (0)
105 static inline void insb_seg(u16 port, u16 segment, u16 offset, u16 count) {
106 SET_SEG(ES, segment);
107 insb(port, (u8*)(offset+0), count);
109 static inline void insw_seg(u16 port, u16 segment, u16 offset, u16 count) {
110 SET_SEG(ES, segment);
111 insw(port, (u16*)(offset+0), count);
113 static inline void insl_seg(u16 port, u16 segment, u16 offset, u16 count) {
114 SET_SEG(ES, segment);
115 insl(port, (u32*)(offset+0), count);
117 static inline void outsb_seg(u16 port, u16 segment, u16 offset, u16 count) {
118 SET_SEG(ES, segment);
119 outsb(port, (u8*)(offset+0), count);
121 static inline void outsw_seg(u16 port, u16 segment, u16 offset, u16 count) {
122 SET_SEG(ES, segment);
123 outsw(port, (u16*)(offset+0), count);
125 static inline void outsl_seg(u16 port, u16 segment, u16 offset, u16 count) {
126 SET_SEG(ES, segment);
127 outsl(port, (u32*)(offset+0), count);