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 #define READ8_SEG(SEG, var) ({ \
11 __asm__ __volatile__("movb %%" #SEG ":%1, %b0" \
12 : "=Qi"(__value) : "m"(var)); \
14 #define READ16_SEG(SEG, var) ({ \
16 __asm__ __volatile__("movw %%" #SEG ":%1, %w0" \
17 : "=ri"(__value) : "m"(var)); \
19 #define READ32_SEG(SEG, var) ({ \
21 __asm__ __volatile__("movl %%" #SEG ":%1, %0" \
22 : "=ri"(__value) : "m"(var)); \
24 #define WRITE8_SEG(SEG, var, value) \
25 __asm__ __volatile__("movb %b0, %%" #SEG ":%1" \
26 : : "Q"(value), "m"(var))
27 #define WRITE16_SEG(SEG, var, value) \
28 __asm__ __volatile__("movw %w0, %%" #SEG ":%1" \
29 : : "r"(value), "m"(var))
30 #define WRITE32_SEG(SEG, var, value) \
31 __asm__ __volatile__("movl %0, %%" #SEG ":%1" \
32 : : "r"(value), "m"(var))
34 extern void __force_link_error__unknown_type();
36 #define __GET_VAR(seg, var) ({ \
38 if (__builtin_types_compatible_p(typeof(__val), u8)) \
39 __val = READ8_SEG(seg, var); \
40 else if (__builtin_types_compatible_p(typeof(__val), u16)) \
41 __val = READ16_SEG(seg, var); \
42 else if (__builtin_types_compatible_p(typeof(__val), u32)) \
43 __val = READ32_SEG(seg, var); \
45 __force_link_error__unknown_type(); \
48 #define __SET_VAR(seg, var, val) do { \
49 if (__builtin_types_compatible_p(typeof(var), u8)) \
50 WRITE8_SEG(seg, var, (val)); \
51 else if (__builtin_types_compatible_p(typeof(var), u16)) \
52 WRITE16_SEG(seg, var, (val)); \
53 else if (__builtin_types_compatible_p(typeof(var), u32)) \
54 WRITE32_SEG(seg, var, (val)); \
56 __force_link_error__unknown_type(); \
59 #define __SET_SEG(SEG, value) \
60 __asm__ __volatile__("movw %w0, %%" #SEG : : "r"(value))
61 #define __GET_SEG(SEG) ({ \
63 __asm__ __volatile__("movw %%" #SEG ", %w0" : "=r"(__seg)); \
66 #define GET_FARVAR(seg, var) ({ \
68 GET_VAR(ES, (var)); })
69 #define SET_FARVAR(seg, var, val) do { \
71 SET_VAR(ES, (var), val); \
74 #define PTR_TO_SEG(p) ((((u32)(p)) >> 4) & 0xf000)
75 #define PTR_TO_OFFSET(p) (((u32)(p)) & 0xffff)
77 #define __GET_FARPTR(ptr) ({ \
78 typeof (&(ptr)) __ptr; \
79 GET_FARVAR(PTR_TO_SEG(__ptr), *(typeof __ptr)PTR_TO_OFFSET(__ptr)); })
80 #define __SET_FARVAR(ptr, val) do { \
81 typeof (&(ptr)) __ptr; \
82 SET_FARVAR(PTR_TO_SEG(__ptr), *(typeof __ptr)PTR_TO_OFFSET(__ptr) \
87 #define GET_VAR(seg, var) __GET_VAR(seg, (var))
88 #define SET_VAR(seg, var, val) __SET_VAR(seg, (var), (val))
89 #define SET_SEG(SEG, value) __SET_SEG(SEG, (value))
90 #define GET_SEG(SEG) __GET_SEG(SEG)
91 #define GET_FARPTR(ptr) __GET_FARPTR(ptr)
92 #define SET_FARPTR(ptr, val) __SET_FARPTR((ptr), (val))
94 // In 32-bit mode there is no need to mess with the segments.
95 #define GET_VAR(seg, var) (var)
96 #define SET_VAR(seg, var, val) do { (var) = (val); } while (0)
97 #define SET_SEG(SEG, value) ((void)(value))
98 #define GET_SEG(SEG) 0
99 #define GET_FARPTR(ptr) (ptr)
100 #define SET_FARPTR(ptr, val) do { (var) = (val); } while (0)