blob: 86179cce38ecc58a5c2143b127d62c1a3e6d6d0a [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// Code to access multiple segments within gcc.
2//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4//
5// This file may be distributed under the terms of the GNU GPLv3 license.
6
7#define READ8_SEG(SEG, var) ({ \
8 u8 __value; \
9 __asm__ __volatile__("movb %%" #SEG ":%1, %b0" \
10 : "=Qi"(__value) : "m"(var)); \
11 __value; })
12#define READ16_SEG(SEG, var) ({ \
13 u16 __value; \
14 __asm__ __volatile__("movw %%" #SEG ":%1, %w0" \
15 : "=ri"(__value) : "m"(var)); \
16 __value; })
17#define READ32_SEG(SEG, var) ({ \
18 u32 __value; \
19 __asm__ __volatile__("movl %%" #SEG ":%1, %0" \
20 : "=ri"(__value) : "m"(var)); \
21 __value; })
22#define WRITE8_SEG(SEG, var, value) \
23 __asm__ __volatile__("movb %b0, %%" #SEG ":%1" \
24 : : "Q"(value), "m"(var))
25#define WRITE16_SEG(SEG, var, value) \
26 __asm__ __volatile__("movw %w0, %%" #SEG ":%1" \
27 : : "r"(value), "m"(var))
28#define WRITE32_SEG(SEG, var, value) \
29 __asm__ __volatile__("movl %0, %%" #SEG ":%1" \
30 : : "r"(value), "m"(var))
31
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050032extern void __force_link_error__unknown_type();
33
Kevin O'Connor4b60c002008-02-25 22:29:55 -050034#define __GET_VAR(seg, var) ({ \
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050035 typeof(var) __val; \
36 if (__builtin_types_compatible_p(typeof(__val), u8)) \
37 __val = READ8_SEG(seg, var); \
38 else if (__builtin_types_compatible_p(typeof(__val), u16)) \
39 __val = READ16_SEG(seg, var); \
40 else if (__builtin_types_compatible_p(typeof(__val), u32)) \
41 __val = READ32_SEG(seg, var); \
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050042 else \
43 __force_link_error__unknown_type(); \
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050044 __val; })
45
Kevin O'Connor4b60c002008-02-25 22:29:55 -050046#define __SET_VAR(seg, var, val) do { \
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050047 if (__builtin_types_compatible_p(typeof(var), u8)) \
48 WRITE8_SEG(seg, var, (val)); \
49 else if (__builtin_types_compatible_p(typeof(var), u16)) \
50 WRITE16_SEG(seg, var, (val)); \
51 else if (__builtin_types_compatible_p(typeof(var), u32)) \
52 WRITE32_SEG(seg, var, (val)); \
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050053 else \
54 __force_link_error__unknown_type(); \
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050055 } while (0)
56
Kevin O'Connor4b60c002008-02-25 22:29:55 -050057#define __SET_SEG(SEG, value) \
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050058 __asm__ __volatile__("movw %w0, %%" #SEG : : "r"(value))
Kevin O'Connor4b60c002008-02-25 22:29:55 -050059#define __GET_SEG(SEG) ({ \
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050060 u16 __seg; \
61 __asm__ __volatile__("movw %%" #SEG ", %w0" : "=r"(__seg)); \
62 __seg;})
63
Kevin O'Connor4b60c002008-02-25 22:29:55 -050064#define GET_FARVAR(seg, var) ({ \
65 SET_SEG(ES, (seg)); \
66 GET_VAR(ES, (var)); })
67#define SET_FARVAR(seg, var, val) do { \
68 SET_SEG(ES, (seg)); \
69 SET_VAR(ES, (var), val); \
70 } while (0)
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050071
72#define PTR_TO_SEG(p) ((((u32)(p)) >> 4) & 0xf000)
73#define PTR_TO_OFFSET(p) (((u32)(p)) & 0xffff)
74
75#define __GET_FARPTR(ptr) ({ \
76 typeof (&(ptr)) __ptr; \
77 GET_FARVAR(PTR_TO_SEG(__ptr), *(typeof __ptr)PTR_TO_OFFSET(__ptr)); })
78#define __SET_FARVAR(ptr, val) do { \
79 typeof (&(ptr)) __ptr; \
80 SET_FARVAR(PTR_TO_SEG(__ptr), *(typeof __ptr)PTR_TO_OFFSET(__ptr) \
81 , (val)); \
82 } while (0)
83
84#ifdef MODE16
85#define GET_VAR(seg, var) __GET_VAR(seg, (var))
86#define SET_VAR(seg, var, val) __SET_VAR(seg, (var), (val))
87#define SET_SEG(SEG, value) __SET_SEG(SEG, (value))
88#define GET_SEG(SEG) __GET_SEG(SEG)
89#define GET_FARPTR(ptr) __GET_FARPTR(ptr)
90#define SET_FARPTR(ptr, val) __SET_FARPTR((ptr), (val))
91#else
92// In 32-bit mode there is no need to mess with the segments.
93#define GET_VAR(seg, var) (var)
94#define SET_VAR(seg, var, val) do { (var) = (val); } while (0)
95#define SET_SEG(SEG, value) ((void)(value))
96#define GET_SEG(SEG) 0
97#define GET_FARPTR(ptr) (ptr)
98#define SET_FARPTR(ptr, val) do { (var) = (val); } while (0)
99#endif