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 { \
72 typeof(var) __sfv_val = (val); \
74 SET_VAR(ES, (var), __sfv_val); \
77 #define PTR_TO_SEG(p) ((((u32)(p)) >> 4) & 0xf000)
78 #define PTR_TO_OFFSET(p) (((u32)(p)) & 0xffff)
80 #define __GET_FARPTR(ptr) ({ \
81 typeof (&(ptr)) __ptr; \
82 GET_FARVAR(PTR_TO_SEG(__ptr), *(typeof __ptr)PTR_TO_OFFSET(__ptr)); })
83 #define __SET_FARVAR(ptr, val) do { \
84 typeof (&(ptr)) __ptr; \
85 SET_FARVAR(PTR_TO_SEG(__ptr), *(typeof __ptr)PTR_TO_OFFSET(__ptr) \
90 #define GET_VAR(seg, var) __GET_VAR(seg, (var))
91 #define SET_VAR(seg, var, val) __SET_VAR(seg, (var), (val))
92 #define SET_SEG(SEG, value) __SET_SEG(SEG, (value))
93 #define GET_SEG(SEG) __GET_SEG(SEG)
94 #define GET_FARPTR(ptr) __GET_FARPTR(ptr)
95 #define SET_FARPTR(ptr, val) __SET_FARPTR((ptr), (val))
97 // In 32-bit mode there is no need to mess with the segments.
98 #define GET_VAR(seg, var) (var)
99 #define SET_VAR(seg, var, val) do { (var) = (val); } while (0)
100 #define SET_SEG(SEG, value) ((void)(value))
101 #define GET_SEG(SEG) 0
102 #define GET_FARPTR(ptr) (ptr)
103 #define SET_FARPTR(ptr, val) do { (var) = (val); } while (0)
106 static inline void insb_seg(u16 port, u16 segment, u16 offset, u16 count) {
107 SET_SEG(ES, segment);
108 insb(port, (u8*)(offset+0), count);
110 static inline void insw_seg(u16 port, u16 segment, u16 offset, u16 count) {
111 SET_SEG(ES, segment);
112 insw(port, (u16*)(offset+0), count);
114 static inline void insl_seg(u16 port, u16 segment, u16 offset, u16 count) {
115 SET_SEG(ES, segment);
116 insl(port, (u32*)(offset+0), count);
118 static inline void outsb_seg(u16 port, u16 segment, u16 offset, u16 count) {
119 SET_SEG(ES, segment);
120 outsb(port, (u8*)(offset+0), count);
122 static inline void outsw_seg(u16 port, u16 segment, u16 offset, u16 count) {
123 SET_SEG(ES, segment);
124 outsw(port, (u16*)(offset+0), count);
126 static inline void outsl_seg(u16 port, u16 segment, u16 offset, u16 count) {
127 SET_SEG(ES, segment);
128 outsl(port, (u32*)(offset+0), count);